first commit
This commit is contained in:
commit
97dd8f8004
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/l
|
||||
*.o
|
||||
lex.yy.c
|
||||
y.tab.[ch]
|
17
Makefile
Normal file
17
Makefile
Normal file
|
@ -0,0 +1,17 @@
|
|||
CC ?= cc
|
||||
CFLAGS= -Wfatal-errors -Wall -Wextra
|
||||
LDFLAGS= -s
|
||||
|
||||
all: l
|
||||
|
||||
l: l.o y.tab.c y.tab.h lex.yy.c
|
||||
cc lex.yy.c y.tab.c -o $@
|
||||
lex.yy.c: l.l
|
||||
lex $^
|
||||
y.tab.c: l.y
|
||||
yacc -d $^
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ $(CFLAGS) $(LDFLAGS)
|
||||
clean:
|
||||
$(RM) l y.tab.c y.tab.h lex.yy.c
|
23
l.l
Normal file
23
l.l
Normal file
|
@ -0,0 +1,23 @@
|
|||
%option noyywrap
|
||||
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#define YY_DECL int yylex()
|
||||
#include "y.tab.h"
|
||||
%}
|
||||
|
||||
%%
|
||||
|
||||
[ \t] ; // ignore all whitespace
|
||||
[0-9]+ {yylval = atoi(yytext); return T_INT;}
|
||||
\n {return T_NEWLINE;}
|
||||
"+" {return T_PLUS;}
|
||||
"-" {return T_MINUS;}
|
||||
"*" {return T_MULTIPLY;}
|
||||
"/" {return T_DIVIDE;}
|
||||
"(" {return T_LEFT;}
|
||||
")" {return T_RIGHT;}
|
||||
"exit" {return T_QUIT;}
|
||||
"quit" {return T_QUIT;}
|
||||
|
||||
%%
|
39
l.y
Normal file
39
l.y
Normal file
|
@ -0,0 +1,39 @@
|
|||
%{
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern int yylex();
|
||||
extern int yyparse();
|
||||
extern FILE* yyin;
|
||||
|
||||
void yyerror(const char* s);
|
||||
%}
|
||||
|
||||
%token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LEFT T_RIGHT
|
||||
%token T_NEWLINE T_QUIT T_INT
|
||||
%start translation_unit
|
||||
|
||||
%%
|
||||
|
||||
translation_unit: line
|
||||
| translation_unit line
|
||||
;
|
||||
|
||||
line: T_INT T_NEWLINE {printf("%d\n",$1);}
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
int main()
|
||||
{
|
||||
yyin = stdin;
|
||||
do yyparse();
|
||||
while(!feof(yyin));
|
||||
}
|
||||
|
||||
void yyerror(const char*s)
|
||||
{
|
||||
fprintf(stderr, "Parse error: %s\n", s);
|
||||
exit(1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user