Untitled
user_2381398
plain_text
2 years ago
7.5 kB
1
Indexable
%{ #include <stdio.h> #include <string.h> #include <stdbool.h> #include <iostream> using namespace std; #define Trace(t) printf(t) #define MAX 256 int yylex(void); void yyerror(const char *s); #define MAX 256 #define MAX_TABLE 1024 struct content { int ival; float fval; bool bval; char id[MAX]; char sval[MAX]; char type[MAX]; char bigtype[MAX]; }; struct content symbolTable[MAX_TABLE][MAX_TABLE]; int scope = 0; int id_total = 0; int insert(struct content a) { // Check if id already in the symbol table for(int i = 0 ; i < id_total; i++) { if(strcmp(a.id, symbolTable[scope][i].id)==0) return 0; } // Copy a's content to symbol table strcpy(symbolTable[scope][id_total].id, a.id); strcpy(symbolTable[scope][id_total].type, a.type); strcpy(symbolTable[scope][id_total].bigtype, a.bigtype); if(strcmp("arrays", a.bigtype) != 0) { if(strcmp("int", a.type) == 0) symbolTable[scope][id_total].ival = a.ival; else if(strcmp("float", a.type) == 0) symbolTable[scope][id_total].fval = a.fval; else if(strcmp("str", a.type) == 0) strcpy(symbolTable[scope][id_total].sval, a.sval); else if(strcmp("bool", a.type) == 0) symbolTable[scope][id_total].bval = a.bval; } id_total++; return 1; } int lookup(char *c){ for (int i = 0; i <= scope; i++) { for(int j=0; j<=id_total; j++){ if (strcmp(c , symbolTable[i][j].id) == 0 && strcmp(symbolTable[i][j].type,"int") == 0) { return symbolTable[i][j].ival; } } } return -1; // Add error handling (e.g., not found) } %} %union{ string* idString; int integerValue; float floatValue; char *strValue; char type[MAX]; } /* tokens */ %token DOT COMMA COLON SEMICOLON LP RP LSB RSB LCB RCB PLUS MINUS MULTIPLY DIVIDE MOD COLONEQUALS LT LE GT GE EQ EQUALS NOTEQUALS AND OR NOT %token ARRAY BOOL CHAR CONST DECREASING DEFAULT TO DO ELSE BEGIN_KEYWORD END EXIT FALSE FOR FUNCTION GET IF ENDIF ENDFOR ENDLOOP INT LOOP OF PUT PROCEDURE REAL RESULT RETURN SKIP STRING THEN TRUE VAR WHEN %token <idString> IDENTIFIER %token <floatValue> FLOATVAL %token <integerValue> INTEGERVAL %token <strValue> STRINGVAL %token <integerValue> TRUEVAL %token <integerValue> FALSEVAL %token <integerValue> INT_RANGE %nonassoc LT LE GT EG NE %nonassoc EQ %left AND %left OR %right NOT %left PLUS MINUS %left MULTIPLY DIVIDE MOD %nonassoc UMINUS //%type<integerValue> unary_expression %start program %% program: program_units {Trace("Reducing to [program]\n");} ; constant_declaration: CONST IDENTIFIER COLON type COLONEQUALS constant_exp { Trace("Reducing to [constant_declaration]\n"); } | CONST IDENTIFIER COLONEQUALS constant_exp { Trace("Reducing to [constant_declaration]\n"); } | CONST IDENTIFIER COLON type COLONEQUALS MINUS constant_exp { Trace("Reducing to [constant_declaration]\n"); } | CONST IDENTIFIER COLONEQUALS MINUS constant_exp { Trace("Reducing to [constant_declaration]\n"); } ; variables_declaration: VAR IDENTIFIER COLON type variables_declaration_tail | VAR IDENTIFIER COLONEQUALS constant_exp { Trace("Reducing to [variables_declaration]\n"); } ; variables_declaration_tail: COLONEQUALS constant_exp { Trace("Reducing to [variables_declaration]\n"); } | variables_declaration_empty ; variables_declaration_empty: /* empty */ { Trace("Reducing to [variables_declaration]\n"); } ; arrays_declaration: VAR IDENTIFIER COLON ARRAY INT_RANGE OF type { Trace("Reducing to [arrays_declaration]\n");} ; program_units: program_unit | program_units program_unit ; program_unit: constant_declaration | variables_declaration | arrays_declaration | function_expression | procedure_expression | statements | loop ; statements: blocks | simple | if_statement ; blocks: BEGIN_KEYWORD program_units END ; simple: IDENTIFIER COLONEQUALS INTEGERVAL | IDENTIFIER COLONEQUALS function_invocation | IDENTIFIER COLONEQUALS IDENTIFIER PLUS INTEGERVAL | IDENTIFIER COLONEQUALS IDENTIFIER PLUS IDENTIFIER | PUT signed_expression | GET IDENTIFIER | RESULT expression | EXIT | EXIT WHEN bool_expression | SKIP | EXIT WHEN IDENTIFIER EQUALS IDENTIFIER ; signed_expression: MINUS IDENTIFIER | IDENTIFIER | STRINGVAL | INTEGERVAL | FLOATVAL | bool_expression | parenthesized_stringval ; parenthesized_stringval: LP STRINGVAL RP ; expression: INTEGERVAL | IDENTIFIER | MINUS INTEGERVAL | function_invocation | expression PLUS expression | expression MINUS expression | expression MULTIPLY expression | expression DIVIDE expression | expression MOD expression | expression LT expression | expression LE expression | expression EQUALS expression | expression EG expression | expression GT expression | expression NOTEQUALS expression | expression NOT expression | expression AND expression | expression OR expression ; function_expression: FUNCTION IDENTIFIER LP formal_arguments RP COLON type program_units END IDENTIFIER { Trace("Reducing to [function_expression]\n"); } ; procedure_expression: PROCEDURE IDENTIFIER LP formal_arguments RP program_units END IDENTIFIER { Trace("Redcing to [procedure_exprssion]\n"); } ; formal_arguments: formal_arguments COMMA formal_argument | formal_argument | ; formal_argument: IDENTIFIER COLON type { Trace("Reducing to [formal_argument]\n"); }; if_statement: IF bool_expression THEN program_units ENDIF {Trace("Reducing to [if_statement]\n");} | IF bool_expression THEN program_units ELSE program_units ENDIF {Trace("Reducing to [if_statement]\n");} ; bool_expression: TRUEVAL | FALSEVAL | expression LT expression | expression LE expression | expression EQ expression | expression GE expression | expression GT expression | expression NE expression | NOT bool_expression | bool_expression AND bool_expression | bool_expression OR bool_expression | LP bool_expression RP ; function_invocation: IDENTIFIER LP expressions RP {Trace("Redcing to [function_invocation]\n");} |IDENTIFIER LP RP {Trace("Redcing to [function_invocation]\n");} ; expressions: expression | expressions COMMA expression ; loop: LOOP program_units ENDLOOP {Trace("Reducing to [loop]\n");} |LOOP ENDLOOP {Trace("Reducing to [loop]\n");} |FOR opt_decreasing IDENTIFIER COLON constant_exp DOT DOT constant_exp program_units_or_empty ENDFOR {Trace("Reducing to [loop]\n");} ; program_units_or_empty: program_units | ; opt_decreasing: DECREASING | ; constant_exp: INTEGERVAL | STRINGVAL | FLOATVAL | TRUEVAL | FALSEVAL | IDENTIFIER ; type: STRING | BOOL | REAL | INT ; %% #include "lex.yy.c" void yyerror(const char *msg) { fprintf(stderr, "%s\n", msg); } int main(int argc, char *argv[]) { if(yyparse()){ yyerror("Parsing error !"); } return 0; }
Editor is loading...