lex and yacc

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
 
T

Thomas Maier-Komor

Gvs said:
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

you can cut the circle by making a forward declaration of a parser using
the keyword %type. Take a look at the info pages of bison. There are
some examples that are also valid for yacc...

Tom
 
C

Coos Haak

Op Wed, 11 May 2005 14:10:58 +0200 schreef Thomas Maier-Komor:
Gvs wrote: ..
you can cut the circle by making a forward declaration of a parser using
the keyword %type. Take a look at the info pages of bison. There are
some examples that are also valid for yacc...

Tom

ANSI C has no idea of beasts like Bison, Python, Yak.
Try the nearest zoo.

Thanks, Coos
 
T

T.M. Sommers

Coos said:
Op Wed, 11 May 2005 14:10:58 +0200 schreef Thomas Maier-Komor:


ANSI C has no idea of beasts like Bison, Python, Yak.
Try the nearest zoo.

comp.compilers is the place to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top