473,480 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

lex and yacc

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


Nov 14 '05 #1
3 2011
Gvs wrote:
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
Nov 14 '05 #2
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
Nov 14 '05 #3
Coos Haak wrote:
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...


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


comp.compilers is the place to go.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6323
by: Profetas | last post by:
Hi. I was wondering how can I access the yacc functions? because when use a normal yacc function such as qstring { yylval.string = strdup(yytext+1); if(yylval.string !='"')...
4
4869
by: Rodrick Brown | last post by:
This might be off topic but can someone give me a quick run down on why tools like lexx/yacc/bison are usefull ? I know what there used for but not sure when they should be used and where ? ...
6
15453
by: Volker Hetzer | last post by:
Hi! We are finding ourselves in a situation where we have to parse several more or less free format text files. In the past, on linux, we had flex and bison for generating very fast parsers for...
1
4239
by: deepusrp | last post by:
Helo everyone, i am doing a project on some graphic tool using qt as the front end. i am using lex and yacc as parser. now i am facing a problem since yacc generates only c code as i want to...
13
4980
by: Berk Birand | last post by:
Hi, I am working on a school project where we use lex/yacc to write a compiler for a fictional (Java-like) language. I have handled all the details about the yacc and lex files, but I still have...
2
7482
by: max.giacometti | last post by:
Hi everybody! I am using lex and yacc to write a vhdl to systemc converter. Lex simply reads the input file and yacc implements grammar and translation. I'd like to be able to make yacc...
2
1938
by: Mohitz | last post by:
Hi Guys, I am facing a peculiar problem with my yacc script. Following is the snippet of the yacc script that i am using. I am using lex as the lexical analyzer. Sample Input :
1
1900
by: neena.usenet | last post by:
I have a gui that reads in a file using C++/yacc. If the file has a parsing error, yacc throws the error to my program and I report it. The problem is at that point, I would like to try to open...
5
2159
by: thomas | last post by:
hello I m writing a simple parser using bison. I just used someone's else c++ grammar, to produce a code beutifier. The thing is , most of the actions for productions would have form {$$ = $1 + $2...
1
2001
by: Robert | last post by:
If the goal is to take a 1-dimensional package of text and produce a tree data structure that is easy to understand and manipulate, and then write C/C++ code that works with that tree - does...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.