Compilers
My problem is that i have to write a program using flex(lexlexical analyzer),bison(yaccparser generator) and devC, and i've never used any of these tools. The teacher wants us to use only devc because it's the one she uses.
Bacically the program is ready in the bison manual and i just have to make it work.Well if anyone can tell me how to create it compile and run it i would be very gratefull. Here's the part of the manual that has the program. MultiFunction Calculator: mfcalc Now that the basics of Bison have been discussed, it is time to move on to a more advanced problem. The above calculators provided only five functions, +, , *, / and ^. It would be nice to have a calculator that provides other mathematical functions such as sin, cos, etc. It is easy to add new operators to the infix calculator as long as they are only singlecharacter literals. The lexical analyzer yylex passes back all nonnumber characters as tokens, so new grammar rules suffice for adding a new operator. But we want something more flexible: builtin functions whose syntax has this form: function_name (argument) At the same time, we will add memory to the calculator, by allowing you to create named variables, store values in them, and use them later. Here is a sample session with the multifunction calculator: $ mfcalc pi = 3.141592653589 3.1415926536 sin(pi) 0.0000000000 alpha = beta1 = 2.3 2.3000000000 alpha 2.3000000000 ln(alpha) 0.8329091229 exp(ln(beta1)) 2.3000000000 $ Note that multiple assignment and nested function calls are permitted. • Decl: Bison declarations for multifunction calculator. • Rules: Grammar rules for the calculator. • Symtab: Symbol table management subroutines. ________________________________________ Node:Mfcalc Decl, Next:Mfcalc Rules, Up:Multifunction Calc Declarations for mfcalc Here are the C and Bison declarations for the multifunction calculator. %{ #include <math.h> /* For math functions, cos(), sin(), etc. */ #include "calc.h" /* Contains definition of `symrec' */ %} %union { double val; /* For returning numbers. */ symrec *tptr; /* For returning symboltable pointers */ } %token <val> NUM /* Simple double precision number */ %token <tptr> VAR FNCT /* Variable and Function */ %type <val> exp %right '=' %left '' '+' %left '*' '/' %left NEG /* Negationunary minus */ %right '^' /* Exponentiation */ /* Grammar follows */ %% The above grammar introduces only two new features of the Bison language. These features allow semantic values to have various data types (see More Than One Value Type). The %union declaration specifies the entire list of possible types; this is instead of defining YYSTYPE. The allowable types are now doublefloats (for exp and NUM) and pointers to entries in the symbol table. See The Collection of Value Types. Since values can now have various types, it is necessary to associate a type with each grammar symbol whose semantic value is used. These symbols are NUM, VAR, FNCT, and exp. Their declarations are augmented with information about their data type (placed between angle brackets). The Bison construct %type is used for declaring nonterminal symbols, just as %token is used for declaring token types. We have not used %type before because nonterminal symbols are normally declared implicitly by the rules that define them. But exp must be declared explicitly so we can specify its value type. See Nonterminal Symbols. ________________________________________ Node:Mfcalc Rules, Next:Mfcalc Symtab, Previous:Mfcalc Decl, Up:Multifunction Calc Grammar Rules for mfcalc Here are the grammar rules for the multifunction calculator. Most of them are copied directly from calc; three rules, those which mention VAR or FNCT, are new. input: /* empty */  input line ; line: '\n'  exp '\n' { printf ("\t%.10g\n", $1); }  error '\n' { yyerrok; } ; exp: NUM { $$ = $1; }  VAR { $$ = $1>value.var; }  VAR '=' exp { $$ = $3; $1>value.var = $3; }  FNCT '(' exp ')' { $$ = (*($1>value.fnctptr))($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 */ %% ________________________________________ Node:Mfcalc Symtab, Previous:Mfcalc Rules, Up:Multifunction Calc The mfcalc Symbol Table The multifunction calculator requires a symbol table to keep track of the names and meanings of variables and functions. This doesn't affect the grammar rules (except for the actions) or the Bison declarations, but it requires some additional C functions for support. The symbol table itself consists of a linked list of records. Its definition, which is kept in the header calc.h, is as follows. It provides for either functions or variables to be placed in the table. Code:
/* Fonctions type. */ Code:
#include <stdio.h> Two important functions allow lookup and installation of symbols in the symbol table. The function putsym is passed a name and the type (VAR or FNCT) of the object to be installed. The object is linked to the front of the list, and a pointer to the object is returned. The function getsym is passed the name of the symbol to look up. If found, a pointer to that symbol is returned; otherwise zero is returned. Code:
symrec * The string is passed to getsym for look up in the symbol table. If the name appears in the table, a pointer to its location and its type (VAR or FNCT) is returned to yyparse. If it is not already in the table, then it is installed as a VAR using putsym. Again, a pointer and its type (which must be VAR) is returned to yyparse. No change is needed in the handling of numeric values and arithmetic operators in yylex. Code:
#include <ctype.h> 
Re: Compilers
Do you have an actual question here, or are you looking for someone to do your work for you?

All times are GMT +5.5. The time now is 16:55. 