Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create clean version for 01scanner, only tree files , Removed useless symbols. #75

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 01_Scanner/cleancode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# I make a clean version for learning.(only tree file)
`if require: chmod +x run.sh`
run it: ./run.sh
20 changes: 20 additions & 0 deletions 01_Scanner/cleancode/const.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int Line;
extern int Putback;
extern FILE* Infile;

typedef struct token{
int type_idx;
int value;
}Token;

enum {
T_PLUS, T_MINUS, T_STAR, T_SLASH, T_INTLIT
};

extern int scan(Token*);

static char const * const token_str[] = { "+", "-", "*", "/", "intlit" };
1 change: 1 addition & 0 deletions 01_Scanner/cleancode/data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 + 3 * 5 /2
34 changes: 34 additions & 0 deletions 01_Scanner/cleancode/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "const.h"

int Line = 0;
int Putback = '\n';
FILE *Infile = NULL;

static void scanfile()
{
Token t;
while (scan(&t))
{
printf("token: %s", token_str[t.type_idx]);
if (t.type_idx == T_INTLIT)
printf(",value: %d", t.value);
putchar('\n');
}
}
int main(int argc, char *argv[])
{
if (argc != 2){
puts("usage:scanner input.txt");
exit(0);
}
Infile = fopen(argv[1], "r");
printf("opend file: %s\n",argv[1]);
if(Infile == NULL){
puts("file not open!");
}
scanfile();
return 0;
}
4 changes: 4 additions & 0 deletions 01_Scanner/cleancode/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

gcc main.c scan.c -o scanner
./scanner data
87 changes: 87 additions & 0 deletions 01_Scanner/cleancode/scan.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "const.h"
#include <ctype.h>
static int chrpos(char *s, int c)
{
char *p;

p = strchr(s, c);
return (p ? p - s : -1);
}
static void putback(int c)
{
Putback = c;
}

static int next()
{
int c;
if (Putback)
{
c = Putback;
Putback = 0;
return c;
}
c = fgetc(Infile);
if (c == '\n')
Line++;
return c;
}
static int scanint(int c)
{
int k, val = 0;

while ((k = chrpos("0123456789", c)) >= 0)
{
val = val * 10 + k;
c = next();
}

putback(c);
return val;
}

static int skip(void)
{
int c;

c = next();
while (' ' == c || '\t' == c || '\n' == c || '\r' == c || '\f' == c)
{
c = next();
}
return (c);
}

int scan(Token *t)
{
int c = skip();
// printf("read char: %c\n", c);
switch (c)
{
case EOF:
return (0);
case '+':
t->type_idx = T_PLUS;
break;
case '-':
t->type_idx = T_MINUS;
break;
case '*':
t->type_idx = T_STAR;
break;
case '/':
t->type_idx = T_SLASH;
break;
default:
if (isdigit(c))
{
t->type_idx = T_INTLIT;
t->value = scanint(c);
break;
}
printf("Unrecognised character %c on line %d\n", c, Line);
exit(1);
}

return 1;
}