LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How flex parses the flex program code. (https://www.linuxquestions.org/questions/programming-9/how-flex-parses-the-flex-program-code-4175732020/)

ajiten 12-21-2023 03:32 AM

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.

pan64 12-21-2023 03:37 AM

probably this? https://stackoverflow.com/questions/...-bison-grammar

ajiten 12-31-2023 04:28 AM

Quote:

Originally Posted by pan64 (Post 6471819)

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.

NevemTeve 12-31-2023 04:46 AM

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.)

ajiten 01-02-2024 07:45 AM

Quote:

Originally Posted by NevemTeve (Post 6473732)
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.

NevemTeve 01-02-2024 08:57 AM

Please instead of "given post" clearly describe problem.

ajiten 01-05-2024 03:56 AM

Quote:

Originally Posted by NevemTeve (Post 6474156)
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.

NevemTeve 01-05-2024 09:37 AM

Your lexical parser never returns ADDOP, MULOP etc

NevemTeve 01-06-2024 02:44 PM

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

ajiten 02-01-2024 12:15 AM

Quote:

Originally Posted by NevemTeve (Post 6475030)

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.

NevemTeve 02-01-2024 01:26 AM

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

pan64 02-01-2024 01:48 AM

Quote:

Originally Posted by ajiten (Post 6480606)
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 (Post 6480606)
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).

ajiten 02-01-2024 04:21 AM

Quote:

Originally Posted by NevemTeve (Post 6480609)
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.


NevemTeve 02-01-2024 07:18 AM

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.

ajiten 02-01-2024 11:31 AM

1 Attachment(s)
Quote:

Originally Posted by NevemTeve (Post 6480655)
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.


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