testing why grammar does not work

This commit is contained in:
Partho Bhattacharya
2025-02-27 14:13:19 -05:00
parent 3ce8cbae59
commit 5626a5c371
5 changed files with 103 additions and 5 deletions

48
calc.h Normal file
View File

@ -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;
}
}

View File

@ -8,9 +8,9 @@
extern int yylex(void); extern int yylex(void);
void yyerror(const char *err); void yyerror(const char *err);
%} %}
/*
%token ID 101 %token ID 101
%token T_INTEGER 201 %token T_INTEGER
%token T_ADDRESS 202 %token T_ADDRESS 202
%token T_BOOLEAN 203 %token T_BOOLEAN 203
%token T_CHARACTER 204 %token T_CHARACTER 204
@ -54,10 +54,19 @@
%token DOT 612 %token DOT 612
%token RESERVE 613 %token RESERVE 613
%token RELEASE 614 %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 // 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 // D : '[' l ']'; //Declaration Lists Brackets can be taken out to get a list of Declarations

View File

@ -6,7 +6,7 @@
%option header-file="flex.h" %option header-file="flex.h"
%{ %{
#include <stdbool.h> #include <stdbool.h>
#include "typedefs.h" //#include "typedefs.h"
#include "grammar.tab.h" #include "grammar.tab.h"
int line_number = 1, column_number = 1; int line_number = 1, column_number = 1;
#ifndef DEBUG #ifndef DEBUG

BIN
parser

Binary file not shown.

41
testGrammar.y Normal file
View File

@ -0,0 +1,41 @@
%{
#include <stdio.h> /* For printf, etc. */
#include <math.h> /* 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 <double> NUM /* Double precision number. */
%token <symrec*> VAR FUN /* Symbol table pointer: variable/function. */
%nterm <double> 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. */
%%