Last active
July 11, 2021 16:39
-
-
Save thosakwe/ebdaabf71f4d51b033a46ceb9c669f59 to your computer and use it in GitHub Desktop.
Dart lex/yacc?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
import 'calc.darty.dart'; | |
%} | |
%% | |
[ \t\n] ; | |
[0-9]+ { yyval = int.parse(yytext); return T_NUMBER; } | |
\( { return T_LPAREN; } | |
\) { return T_RPAREN; } | |
\* { return T_TIMES; } | |
\/ { return T_SLASH; } | |
\% { return T_PERCENT; } | |
\+ { return T_PLUS; } | |
\- { return T_MINUS; } | |
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
import 'dart:io'; | |
import 'calc.dartl.dart'; | |
%} | |
%token<int> T_NUMBER "number" | |
%token<int> T_TIMES "*" | |
%token<int> T_SLASH "/" | |
%token<int> T_PERCENT "%" | |
%token<int> T_PLUS "+" | |
%token<int> T_MINUS "-" | |
%type<int> expr | |
%% | |
expr | |
"number" { $$ = yylval; } | |
| expr "*" expr { $$ = $1 * $2; } | |
| expr "/" expr { $$ = $1 * $2; } | |
| expr "%" expr { $$ = $1 * $2; } | |
| expr "+" expr { $$ = $1 * $2; } | |
| expr "-" expr { $$ = $1 * $2; } | |
| "(" expr ")" { $$ = $2; } | |
%% | |
void yyerror(String msg) { | |
stderr.writeln(msg); | |
exit(1); | |
} | |
void main() { | |
while(true) { | |
String line; | |
stdout.write("calc) "); | |
line = stdin.readLineSync(); | |
yyparse(line); | |
stdout.writeln(yylval); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment