I'm working on some practice programs with Lex/Yacc and having some problems ( I haven't received my copy of Lex & Yacc in the mail yet , damn eBay! ). I thought that it would be fun to write a parser for entering a system of linear equations from the console as practice ( and then send the results to some C code I wrote to do Guassian Elimination on the system and display the answers. Anyway , heres what I want to enter ( parse ):
3x + 3y = 3
3x - 3y = 2
x + 2y = 3
heres my lex and yacc scripts for a single equation ( just one ( 3x - 2y = 3 ) or something :
%{
#define end 260
#define equ 261
#define trm 262
#define num 263
#define pls 264
#define mun 265
#define otr 266
#define nl 267
#include <stdio.h>
#include <stdlib.h>
%}
end "end"
term [0-9]*[A-Za-z]
number [0-9]+
plus "+"
minus "-"
equal "="
ws [ \t]+
newline [\n]
other .
%%
{ws} ;
{newline} { return nl; }
{term} { return trm; }
{plus} { return pls; }
{minus} { return mun; }
{equal} { return equ; }
{end} { return end; }
{number} { return num; }
{other} { return otr; }
%%
#ifdef debug
main ()
{
int ch;
do
{
ch = yylex();
if ( ch == trm )
printf ( "term\n" );
else if ( ch == pls )
printf ( "plus\n" );
else if ( ch == mun )
printf ( "minus\n" );
else if ( ch == equ )
printf ( "equal" );
else if ( ch == num )
printf ( "number\n" );
else if ( ch == nl )
printf ( "newline\n" );
} while ( ch != end );
}
#endif
int yywrap ()
{
return 1;
}
%{
#include "c:\programs\mat.cpp"
#include <stdio.h>
void yyerror (char const *);
%}
%token end trm num pls min equ otr nl
%start stmt
%%
stmt : expr equ num nl { printf ( "statement" );}
;
expr : expr pls expr { printf ( "expression" ); }
| expr min expr
| trm
;
%%
#include <stdio.h>
void main ()
{
do
{
yyparse();
}while ( !feof( stdin ) );
}
void
yyerror (char const *s)
{
fprintf (stderr, "%s\n", s);
}
Any suggestions? Point me in the right direction! Thanks :bg
QuoteI haven't received my copy of Lex & Yacc in the mail yet , damn eBay!
But lex and yacc are free !!
Quote from: striker on May 19, 2005, 02:48:50 AM
QuoteI haven't received my copy of Lex & Yacc in the mail yet , damn eBay!
But lex and yacc are free !!
Presumably, he means the book :-)
Cheers,
Randy Hyde
Doh !
Yeah , I meant the book. Any ideas on the code? :bg
If I define my lexical tokens like this:
end "end"
term [0-9]*[A-Za-z] /* return trm */
number [0-9]+ /* return num */
plus "+" /* return pls */
minus "-" /* return min */
equal "=" /* return equ */
ws [ \t]+
newline [\n] /* return nl */
other .
I should be able to define a equation in the system like this
( right? ):
stmt : expr equ num nl { printf ( "statement" );}
;
expr : expr pls expr { printf ( "expression" ); }
| expr min expr
| trm
;
This for an equation that looks like this:
3x + 4y - 2z = 3 <newline>
I'm I correct here? Thanks.....
expr is ambiguous.
You haven't specified precedence or associativity. I don't know if Yacc has defaults for these things.
Thanks tenkey. I defined the addition and subtraction operator:
%left pls /* plus operator */
%left min /* minus operator */
but I still get "syntax error" from the default error handler when I type in something like:
4c + 4d = 4
must be doing something wrong.....
Hmmm..... VC++ 6 is giving me these warnings :
--------------------Configuration: mat.tab - Win32 Debug--------------------
Compiling...
mat.tab.c
linsys.l(2) : warning C4005: 'end' : macro redefinition
c:\programs\mat.tab.c(66) : see previous definition of 'end'
linsys.l(3) : warning C4005: 'equ' : macro redefinition
c:\programs\mat.tab.c(73) : see previous definition of 'equ'
linsys.l(4) : warning C4005: 'trm' : macro redefinition
c:\programs\mat.tab.c(67) : see previous definition of 'trm'
linsys.l(5) : warning C4005: 'num' : macro redefinition
c:\programs\mat.tab.c(68) : see previous definition of 'num'
linsys.l(6) : warning C4005: 'pls' : macro redefinition
c:\programs\mat.tab.c(71) : see previous definition of 'pls'
linsys.l(8) : warning C4005: 'otr' : macro redefinition
c:\programs\mat.tab.c(69) : see previous definition of 'otr'
linsys.l(9) : warning C4005: 'nl' : macro redefinition
c:\programs\mat.tab.c(70) : see previous definition of 'nl'
c:\program files\microsoft visual studio\vc98\include\stdlib.h(428) : warning C4005: 'min' : macro redefinition
c:\programs\mat.tab.c(72) : see previous definition of 'min'
mat.tab.c(1115) : warning C4102: 'yyerrlab1' : unreferenced label
Linking...
mat.tab.exe - 0 error(s), 9 warning(s)
Seems all the tokens listed in my Bison script. Could this have anything to do with the problem? Maybe I didn't include something I should have :(.........
I got it to work! :bg I forgot to include the file "y.tab.h" in my flex script. I knew it was something like that! Thanks for the help! :U
I got my matrix parser to work using this grammer:
%%
matrix : equationList end { printf ( "matrix defined\n" ); }
;
equationList : equation nl
| equation nl equationList
;
equation : expr equ num
;
expr : trm
| expr min expr
| expr pls expr
;
%%
Pretty neat! Now I can enter a matrix like this:
3c + 4d = 3
3c - d = 3
end
I have to yet get my copy of Lex & Yacc to see how to use the input to produce something I can pass to a set of C algorithms for matrixes. Let you know how it goes.... :bg
Hmmm.... How do I "tell" Flex that :
(-5x)
and
( - 5x )
are the same thing? I currently have a signed term defined as:
signedTerm [(][+|-][0-9]*[A-Za-z][)]
Thanks for any help. :toothy
Opps! Just do this:
sterm [(][ ]*[+|-][ ]*[0-9]*[A-Za-z][ ]*[)]