G
Gvs
Hi,
i'm trying to implement a few things in lex and yacc though i'm having
trouble.
What i want to do is be able to parse a term and an expression.
in EBNF my term is defined as: term ::= factor { '*' | '\' } .
factor is defined as: factor ::= ident | '(' expression ')' | number .
expression is defined as: expression ::= '+' | '-' term { '+' '-' term } .
ident is defined as: ident ::= 'a..z. A..Z' { 'a..z A..Z 0..9' } .
number is defined as: number ::= { '0..9' } .
the problem is i don't know how to include the recursion aspects of
expression, factor and ter
i have defined number and ident in lex as follows
ident [a-zA-Z][A-Za-z0-9]*
number [0-9]+
so to do factor should i do something in LEX like
factor [ident|number|expression]
or do i implement it in Yacc in a rule like
commands:
| commands command
;
command:
factor
;
factor:
IDENT
{
print("factor");
}
|
NUMBER
{
printf("factor");
}
|
LEFTPARENTH EXPRESSION RIGHTPARENTH
{
printf("factor");
}
;
If that is the case where do i define expression in the first place, it's
like one big circle.
I'm really confused .. so any help would be much appreciated.!!
Kind Regards,
Gvs
i'm trying to implement a few things in lex and yacc though i'm having
trouble.
What i want to do is be able to parse a term and an expression.
in EBNF my term is defined as: term ::= factor { '*' | '\' } .
factor is defined as: factor ::= ident | '(' expression ')' | number .
expression is defined as: expression ::= '+' | '-' term { '+' '-' term } .
ident is defined as: ident ::= 'a..z. A..Z' { 'a..z A..Z 0..9' } .
number is defined as: number ::= { '0..9' } .
the problem is i don't know how to include the recursion aspects of
expression, factor and ter
i have defined number and ident in lex as follows
ident [a-zA-Z][A-Za-z0-9]*
number [0-9]+
so to do factor should i do something in LEX like
factor [ident|number|expression]
or do i implement it in Yacc in a rule like
commands:
| commands command
;
command:
factor
;
factor:
IDENT
{
print("factor");
}
|
NUMBER
{
printf("factor");
}
|
LEFTPARENTH EXPRESSION RIGHTPARENTH
{
printf("factor");
}
;
If that is the case where do i define expression in the first place, it's
like one big circle.
I'm really confused .. so any help would be much appreciated.!!
Kind Regards,
Gvs