Last active
August 29, 2015 14:18
-
-
Save jogonba2/775862bfb2217a273e53 to your computer and use it in GitHub Desktop.
CMinilan.y
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
// Author: Overxfl0w13 // | |
%token BEGGIN END PLUS MINUS MULT DIV PRINT EOS LP RP INTEGER FLOAT VARIABLE | |
%left GT LT EQ | |
%left '+' '-' | |
%left '*' '/' | |
%{ | |
#include <stdio.h> | |
int yylex(void); | |
void yyerror(char *); | |
float result; | |
int sym[26]; | |
%} | |
%% | |
program: | |
BEGGIN instructions END { printf("PARSER:: program <== BEGIN instructions END\n"); } | |
; | |
instructions: | |
instructions instruction { printf("PARSER:: instructions <== instructions instruction\n"); } | |
| instruction { printf("PARSER:: instructions <== instruction\n"); } | |
; | |
instruction: | |
sentence EOS { printf("PARSER:: instruction <== sentence EOS\n"); } | |
; | |
sentence: | |
printSentence { printf("PARSER:: sentence <== printSentence\n"); } | |
| VARIABLE '=' arithExpr { | |
printf("PARSER:: VARIABLE <== arithExpr\n"); | |
sym[$1] = $3; | |
} | |
; | |
printSentence: | |
PRINT LP arithExpr RP { | |
printf("PARSER:: printSentence <== PRINT LP arithExpr RP\n"); | |
} | |
; | |
arithExpr: | |
arithExpr PLUS term { | |
printf("PARSER:: arithExpt <== arithExpr PLUS term\n"); | |
result = $1 + $3; | |
printf("Result: %f\n",result); | |
} | |
| arithExpr MINUS term { | |
printf("PARSER:: program <== BEGIN instructions END\n"); | |
result = $1 - $3; | |
printf("Result: %f\n",result); | |
} | |
| term { printf("PARSER:: program <== BEGIN instructions END\n"); } | |
; | |
term: | |
term MULT factor { | |
printf("PARSER:: term <== term MULT factor\n"); | |
result = $1 * $3; | |
printf("Result: %f\n",result); | |
} | |
| term DIV factor { | |
printf("PARSER:: term <== term DIV factor\n"); | |
result = $1 / $3; | |
printf("Result: %f\n",result); | |
} | |
| factor { printf("PARSER:: term <== factor\n"); } | |
; | |
factor: | |
INTEGER { printf("PARSER:: factor <== NUMBER\n"); } | |
| FLOAT { printf("PARSER:: factor <== FLOAT\n"); } | |
| VARIABLE { printf("PARSER:: factor <== VARIABLE\n"); } | |
| LP arithExpr RP { printf("PARSER:: factor <== LP arithExpr RP\n"); } | |
| PLUS LP arithExpr RP { printf("PARSER:: factor <== PLUS LP arithExpr RP\n"); } | |
| MINUS LP arithExpr RP { printf("PARSER:: factor <== MINUS LP arithExpr RP\n"); } | |
| PLUS INTEGER { printf("PARSER:: factor <== PLUS INTEGER\n"); } | |
| PLUS FLOAT { printf("PARSER:: factor <== PLUS FLOAT\n"); } | |
| MINUS INTEGER { printf("PARSER:: factor <== MINUS INTEGER\n"); } | |
| MINUS FLOAT { printf("PARSER:: factor <== MINUS FLOAT\n"); } | |
; | |
%% | |
void yyerror(char *s){ | |
fprintf(stderr,"%s\n",s); | |
} | |
int main(void){ | |
yyparse(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment