diff --git a/calc.h b/calc.h new file mode 100644 index 0000000..2580e93 --- /dev/null +++ b/calc.h @@ -0,0 +1,48 @@ +/* Function type. */ +typedef double (func_t) (double); +/* Data type for links in the chain of symbols. */ +struct symrec +{ + char *name; /* name of symbol */ + int type; /* type of symbol: either VAR or FUN */ + union + { + double var; /* value of a VAR */ + func_t *fun; /* value of a FUN */ + } value; + struct symrec *next; /* link field */ +}; +typedef struct symrec symrec; + +/* The symbol table: a chain of 'struct symrec'. */ +extern symrec *sym_table; + +symrec *putsym (char const *name, int sym_type); +symrec *getsym (char const *name); +struct init +{ + char const *name; + func_t *fun; +}; +struct init const funs[] = +{ + { "atan", atan }, + { "cos", cos }, + { "exp", exp }, + { "ln", log }, + { "sin", sin }, + { "sqrt", sqrt }, + { 0, 0 }, +}; +/* The symbol table: a chain of 'struct symrec'. */ +symrec *sym_table; +/* Put functions in table. */ +static void +init_table (void) +{ + for (int i = 0; funs[i].name; i++) + { + symrec *ptr = putsym (funs[i].name, FUN); + ptr->value.fun = funs[i].fun; + } +} diff --git a/grammar.y b/grammar.y index 2bd520d..0b31edb 100644 --- a/grammar.y +++ b/grammar.y @@ -8,9 +8,9 @@ extern int yylex(void); void yyerror(const char *err); %} -/* + %token ID 101 -%token T_INTEGER 201 +%token T_INTEGER %token T_ADDRESS 202 %token T_BOOLEAN 203 %token T_CHARACTER 204 @@ -54,10 +54,19 @@ %token DOT 612 %token RESERVE 613 %token RELEASE 614 -%token COMMENT 700 */ +%token COMMENT 700 %% -start: /*empty for now*/ +input: /*empty for now*/ + OPTIONAL; + +OPTIONAL_OPTIONAL: + OPTIONAL + | OPTIONAL_OPTIONAL OPTIONAL {printf("expansion\n");}; + +OPTIONAL: + '[' {printf("success");} + |%empty ; // B : '{'{CreateScope(cur,line,column)} E '}'; //Braced Expressions can have braces removed to get regular expressions // D : '[' l ']'; //Declaration Lists Brackets can be taken out to get a list of Declarations diff --git a/lexicalStructure.lex b/lexicalStructure.lex index f4fb603..3bd0332 100644 --- a/lexicalStructure.lex +++ b/lexicalStructure.lex @@ -6,7 +6,7 @@ %option header-file="flex.h" %{ #include - #include "typedefs.h" + //#include "typedefs.h" #include "grammar.tab.h" int line_number = 1, column_number = 1; #ifndef DEBUG diff --git a/parser b/parser index 6d64eed..432a493 100755 Binary files a/parser and b/parser differ diff --git a/testGrammar.y b/testGrammar.y new file mode 100644 index 0000000..04f8f17 --- /dev/null +++ b/testGrammar.y @@ -0,0 +1,41 @@ +%{ + #include /* For printf, etc. */ + #include /* For pow, used in the grammar. */ + #include "calc.h" /* Contains definition of 'symrec'. */ + int yylex (void); + void yyerror (char const *); +%} +%define api.value.type union /* Generate YYSTYPE from these types: */ +%token NUM /* Double precision number. */ +%token VAR FUN /* Symbol table pointer: variable/function. */ +%nterm exp +%precedence '=' +%left '-' '+' +%left '*' '/' +%precedence NEG /* negation--unary minus */ +%right '^' /* exponentiation */ +%% /* The grammar follows. */ +input: + %empty +| input line +; +line: + '\n' +| exp '\n' { printf ("%.10g\n", $1); } +| error '\n' { yyerrok; } +; +exp: + NUM +| VAR { $$ = $1->value.var; } +| VAR '=' exp { $$ = $3; $1->value.var = $3; } +| FUN '(' exp ')' { $$ = $1->value.fun ($3); } +| exp '+' exp { $$ = $1 + $3; } +| exp '-' exp { $$ = $1 - $3; } +| exp '*' exp { $$ = $1 * $3; } +| exp '/' exp { $$ = $1 / $3; } +| '-' exp %prec NEG { $$ = -$2; } +| exp '^' exp { $$ = pow ($1, $3); } +| '(' exp ')' { $$ = $2; } +; +/* End of grammar. */ +%%