LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2023, 03:32 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
How flex parses the flex program code.


Request inputs on how to see the parsing of flex program, by its parser.
The file here seems need be looked for clue, but unable to find which part to look into.

By parsing, mean the specific format in which a flex file is organised, and the yywrap(), yylex() functions as used specific to flex.

Last edited by ajiten; 12-21-2023 at 03:48 AM.
 
Old 12-21-2023, 03:37 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,923

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
probably this? https://stackoverflow.com/questions/...-bison-grammar
 
1 members found this post helpful.
Old 12-31-2023, 04:28 AM   #3
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by pan64 View Post
My post asked about the actual code parsing the flex file, including the yywrap(), yylex() functions as used in flex file.

Also, can you please kindly tell what is causing the error, in the given post; as am unable to find the exact token in the input.txt file, where the error occurred.
 
Old 12-31-2023, 04:46 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Sorry, what exactly the question is? (If it is "who is willing to explain me in details the working of flex program?" then I am not voluntering.)
 
Old 01-02-2024, 07:45 AM   #5
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Sorry, what exactly the question is? (If it is "who is willing to explain me in details the working of flex program?" then I am not voluntering.)
Please take the question as the second part, i e.:

Also, can you please kindly tell what is causing the error, in the given post; as am unable to find the exact token in the input.txt file, where the error occurred.
 
Old 01-02-2024, 08:57 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Please instead of "given post" clearly describe problem.
 
Old 01-05-2024, 03:56 AM   #7
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Please instead of "given post" clearly describe problem.
In the input.txt file as given here:

Code:
int var(int a, int b){
    return a+b;
}
am unable to find where the error occured.


The error is shown as:
Code:
type_specifier -> INT
type_specifier -> INT
parameter_list -> type_specifier ID
type_specifier -> INT
parameter_list -> parameter_list COMMA type_specifier ID
variable -> ID
factor -> variable
unary_expression -> factor
term -> unary_expression
simple_expression -> term
rel_expression  -> simple_expression 
expression -> logic_expression
syntax error, unexpected ID, expecting SEMICOLON
rather than the expected result of:
Code:
type_specifier -> INT
type_specifier -> INT
parameter_list -> type_specifier ID
type_specifier -> INT
parameter_list -> parameter_list COMMA type_specifier ID
variable -> ID
factor -> variable
unary_expression -> factor
term -> unary_expression
simple_expression -> term

variable -> ID
factor -> variable
unary_expression -> factor
term -> unary_expression
simple_expression : simple_expression ADDOP term
rel_expression  -> simple_expression 
logic_expression : rel_expression
expression -> logic_expression
statement : RETURN expression SEMICOLON
statements : statement
compound_statement : LCURL statements RCURL
func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement
unit : func_definition
program : program unit
start : program
---------------------------------------------------------------------------------------------------------------------------------------

Next, the code for implementation of the functions specific to flex, i.e. yymore(), yywrap(), yylex(), is found now here.
But, assume that the flex would have been created much before the C++ was created, hence there must be a C implementation of flex too.
Request some link for the same.
 
Old 01-05-2024, 09:37 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Your lexical parser never returns ADDOP, MULOP etc

Last edited by NevemTeve; 01-05-2024 at 11:02 PM.
 
1 members found this post helpful.
Old 01-06-2024, 02:44 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Sort of fixed it, see here: https://github.com/lzsiga/stackoverflow_50821203

Last edited by NevemTeve; 01-07-2024 at 11:13 AM.
 
1 members found this post helpful.
Old 02-01-2024, 12:15 AM   #10
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Sorry, but if could please detail how to run on cygwin platform, then would be highly thankful.
Actually, the problem was there since you posted it; but failed to muster courage (to ask); & could muster only on the need for following the same approach is given in the link here. The link is derived from the course page here.
The code for the book (preface, code (though in the earlier course web-page too)) followed by the course is also given here.

Am unsure of how much seriously I am being taken of by you, but would still venture to go further by requesting on how to run the code (for the same project, as the stated book) here.

I hope that with your kind help, the next semester would see me helping my students, in building a compiler in far better way than before.

Sorry, as didn't put efforts on my own, but seem to be confused more, and wanted to end the stalemate; rather than not providing any useful inputs to students, the next semester too.

--------------

P.S.: Want to add that have used make files for simpler uses, than specified here; and cannot hence progress.

Last edited by ajiten; 02-01-2024 at 12:53 AM.
 
Old 02-01-2024, 01:26 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
As a start enter command `make parser`
Mind you, this program is incomplete, it only does [partial] lexical parsing.

Last edited by NevemTeve; 02-01-2024 at 01:42 AM.
 
1 members found this post helpful.
Old 02-01-2024, 01:48 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,923

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
Quote:
Originally Posted by ajiten View Post
Am unsure of how much seriously I am being taken of by you, but would still venture to go further by requesting on how to run the code (for the same project, as the stated book) here.

I hope that with your kind help, the next semester would see me helping my students, in building a compiler in far better way than before.
For me it is not really acceptable. You are trying to teach your students something that you have no idea what it is. How can it be taken seriously? How seriously do you take your own students?
The best thing you can do is to learn it.
But you wrote:
Quote:
Originally Posted by ajiten View Post
Sorry, as didn't put efforts on my own, but seem to be confused more, and wanted to end the stalemate; rather than not providing any useful inputs to students, the next semester too.
What definitely won't work is gathering information here (or wherever) for the next semester and waiting for a miracle to at least make it seem like it will work.
But at least you started to ask, which is a good thing. Now you need to understand it too (that will end the stalemate).
 
Old 02-01-2024, 04:21 AM   #13
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
As a start enter command `make parser`
Mind you, this program is incomplete, it only does [partial] lexical parsing.
It does not work, as gives error:
Code:
$ make parser
make: *** No rule to make target 'parser'.  Stop.
 
Old 02-01-2024, 07:18 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Here is how it goes in Linux:
Code:
/tmp$ git clone https://github.com/lzsiga/stackoverflow_50821203.git
Cloning into 'stackoverflow_50821203'...
remote: Enumerating objects: 56, done.
remote: Counting objects: 100% (56/56), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 56 (delta 27), reused 46 (delta 20), pack-reused 0
Unpacking objects: 100% (56/56), done.

/tmp$ cd stackoverflow_50821203/

/tmp/stackoverflow_50821203$ make parser
bison -o parser.c -Hparser.h parser.y
flex -o lexpars.c lexpars.flex
cc -m64 -g -W -Wall -DYYDEBUG -m64 -g -L/usr/local/lib64 -Wl,-rpath,/usr/local/lib64 parser.c lexpars.c   -o parser

/tmp/stackoverflow_50821203$ ./parser sample01.txt
type_specifier -> INT
...cut lines...
start -> program
PS: I have just fixed some compilation warnings.

Last edited by NevemTeve; 02-01-2024 at 07:30 AM. Reason: warnings have been fixed since
 
1 members found this post helpful.
Old 02-01-2024, 11:31 AM   #15
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Here is how it goes in Linux:
Code:
/tmp$ git clone https://github.com/lzsiga/stackoverflow_50821203.git
Cloning into 'stackoverflow_50821203'...
remote: Enumerating objects: 56, done.
remote: Counting objects: 100% (56/56), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 56 (delta 27), reused 46 (delta 20), pack-reused 0
Unpacking objects: 100% (56/56), done.

/tmp$ cd stackoverflow_50821203/

/tmp/stackoverflow_50821203$ make parser
bison -o parser.c -Hparser.h parser.y
flex -o lexpars.c lexpars.flex
cc -m64 -g -W -Wall -DYYDEBUG -m64 -g -L/usr/local/lib64 -Wl,-rpath,/usr/local/lib64 parser.c lexpars.c   -o parser

/tmp/stackoverflow_50821203$ ./parser sample01.txt
type_specifier -> INT
...cut lines...
start -> program
PS: I have just fixed some compilation warnings.
There is only difference in the first line, i.e. in the old make file it is:
Code:
CPPFLAGS := -DYYDEBUG -DYY_NO_UNPUT -DYY_NO_INPUT
But, that make file cannot be used to run the command:
Code:
make parse
The new make file has the given line as:
Code:
CPPFLAGS := -DYYDEBUG
But, the command runs. The difference is shown in the attachment.
Please tell the reason, for the change in output.
Attached Thumbnails
Click image for larger version

Name:	Screenshot (139).png
Views:	8
Size:	203.9 KB
ID:	42506  
 
  


Reply

Tags
parser



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Realtek rtl8822be blues on the new Lenovos.. Flex 6-ARR, Flex 14 AMD Ryzen 2200U specifically w00tus Linux - Newbie 7 10-13-2018 12:30 PM
[Flex & Bison] How to check which state Flex is in? courteous Programming 0 06-03-2011 11:46 AM
help with a shell script that data parses /usr/bin/time Ryzol Programming 1 08-04-2008 04:24 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
flex c++ code about nest file?? chuanyung Programming 0 01-20-2004 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration