Skip to content

Commit b1b0cbe

Browse files
2 parents ac04bd4 + a9970e3 commit b1b0cbe

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
all:
2+
yacc -d simplecalc.y
3+
lex simplecalc.l
4+
gcc y.tab.c lex.yy.c -o simplecalc
5+
6+
clean:
7+
rm -rf simplecalc
8+
rm -rf lex.yy.c
9+
rm -rf y.tab.c
10+
rm -rf y.tab.h
11+
rm -rf y.tab.h.gch
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%{
2+
#include "y.tab.h"
3+
extern int yylval;
4+
5+
%}
6+
7+
%option noyywrap
8+
9+
10+
%%
11+
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
12+
[a-z] { yylval = yytext[0]; return NAME; }
13+
[ \t] ; /* ignore whitespace */
14+
\n return 0; /* logical EOF */
15+
. return yytext[0];
16+
%%
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
%{
2+
#include <stdio.h>
3+
int yylex();
4+
void yyerror (char const *s) {
5+
fprintf (stderr, "%s\n", s);
6+
}
7+
%}
8+
9+
10+
%token NAME NUMBER
11+
%%
12+
13+
statement: NAME '=' expression {printf("%c = %d\n", $1,$3);}
14+
| expression {printf("= %d\n", $1);}
15+
;
16+
expression: expression '+' NUMBER { $$ = $1 + $3;}
17+
| expression '-' NUMBER { $$ = $1 - $3;}
18+
| expression '*' NUMBER { $$ = $1 * $3;}
19+
| '(' expression ')' {$$ = $2;}
20+
| NUMBER {$$ = $1;}
21+
;
22+
23+
%%
24+
25+
int main(){
26+
yyparse();
27+
return 0;
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
%{
2+
#include <stdio.h>
3+
%}
4+
5+
%option noyywrap
6+
7+
%%
8+
[a-zA-Z][a-zA-Z0-9]* { printf("WORD "); }
9+
[a-zA-Z0-9\/.-]+ { printf("FILENAME "); }
10+
\" { printf("QUOTE "); }
11+
\{ { printf("OBRACE "); }
12+
\} { printf("EBRACE "); }
13+
; { printf("SEMICOLON "); }
14+
\n { printf("\n"); }
15+
[ \t]+ /* ignore whitespace */
16+
%%
17+
18+
int main() {
19+
yylex();
20+
return 0;
21+
}

0 commit comments

Comments
 (0)