• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Flex help....

Sber

n00b
Joined
Mar 12, 2012
Messages
3
Hey guys, noob here in dire need of help.

I am preparing for an exam on Compilers and I have serious trouble answering some --seemingly simple --- questions concerning the FLEX logical analyser.
I know, the first thing is for me is to go back to my notes and try harder, but sadly my lecturer just makes little sence to me (and others in the class, to that extent). I have read a few things on the Wikipedia article (doesn't help much) and I would like to ask any experts for help on these two questions.

Here's the questions:

1(i) In a programming language, we declare variables as follows:

var name1,name2,.... : integer;
othername1,othername2,... : real;
otherothername1,... : boolean;
other4name1,... : char;

(a) Consider the declaration

var number_of_attendants, sum: integer;
ticket_price: real;
symbols: char;

(b) Construct the syntactic diagram (this is pretty frustrating, arrgh!)
(c) Define the variables in Backus-Naur form.
(d) Write the associated LEX program in C.
(e) Define the tokens, lexemes and patterns existing in this process.
(e) Define a formal expression regarding variable declaration in this language
and construct the relevant finite automaton.



Ok, and the second one goes...

2) A subset of a natural language contains these sentences:
1) Triangle ABC.
2) Line Segment AB.
3) Angle A.
In this set, names of triangles are formed by putting three letters together,
all drawn from the alphabet A,B,C,D,E. Line segment names are defined by
putting two letters together from the previous alphabet. And angle names are
(suprise!) given by any letter in that alphabet.

a) Write a LEX program (i know this is in C) that identifies words from the previous set of phrases, such that an input of the form "triangle BCD" returns:
-Triangle: a geometric entity (lol at that)
-BCD: name of a geometric entity
b) Define the tokens, lexemes and patterns to be found in formal expressions concerning these three sentences.
c) Construct the finite automaton that corresponds to the formal expressios to describe the names of geometric entities in that language. Use this to see if the name AYZ is recognizable.


Here they are. I would really appreciate any help...

Thanks in advance!
 
Hey guys, noob here in dire need of help.

I am preparing for an exam on Compilers and I have serious trouble answering some --seemingly simple --- questions concerning the FLEX logical analyser.
I know, the first thing is for me is to go back to my notes and try harder, but sadly my lecturer just makes little sence to me (and others in the class, to that extent). I have read a few things on the Wikipedia article (doesn't help much) and I would like to ask any experts for help on these two questions.

Here's the questions:

1(i) In a programming language, we declare variables as follows:

var name1,name2,.... : integer;
othername1,othername2,... : real;
otherothername1,... : boolean;
other4name1,... : char;

(a) Consider the declaration

var number_of_attendants, sum: integer;
ticket_price: real;
symbols: char;

(b) Construct the syntactic diagram (this is pretty frustrating, arrgh!)
(c) Define the variables in Backus-Naur form.
(d) Write the associated LEX program in C.
(e) Define the tokens, lexemes and patterns existing in this process.
(e) Define a formal expression regarding variable declaration in this language
and construct the relevant finite automaton.

Here they are. I would really appreciate any help...

Thanks in advance!

Definitely no expert here, but I did just do this in my prog lang class, so maybe I can try to help a little (not sure about the second problem).

So the pattern of the grammar is something like

Code:
declarations -> VAR variables

Code:
variables -> VarString COLON vartype SEMICOLON variables
                 | VarString COMMA variables
                 | VarString COLON vartype SEMICOLON

Code:
vartype -> INTEGER
               | REAL
               | CHAR
So if you aren't used to this form let me explain it like this. You start at declarations, which is just a label for that grammar section. Declarations starts with a VAR token (tokens being in caps) and then is followed by variables, which is another label for another section. The variables section can contain VarString (which would be regex'd as some kind of string that goes until whitespace/comma) then it can go three different ways:
1) Be the last (or only) declaration for that type (but not the last declaration for everything) so it would just be
VarString COLON vartype SEMICOLON variables
2) Be one declaration, but not the only for that type so it would be
VarString COMMA variables
(meaning it would call variables again, and then can choose any of the options)
3) Be the last (or only) declaration for that type AND for the entire thing and be
VarString COLON vartype SEMICOLON

Then simply vartype is expanded and can be any of the types that are avail (char, int, etc). So what happens is that when the lexer parses the tokens then passes them to the semantic analyzer to make sure the form of the statements is correct (or makes sense)

So take the first line:

var number_of_attendants, sum: integer;

And going through the grammar above, you can see okay matched VAR token then we move on to variables. It will match "number_of_attendants" as a VarString, then match the COMMA and say okay we have more variables so we take option 2 and continue going, get the second VarString "sum" then we find a COLON so we have to take option 1 or 3 (1 in this case because this isn't the end). From there we find a vartype token matching "INTEGER" and then we match the SEMICOLON at the end, but then we see we have more declarations so we take option 1 because it is not the end of the declarations.

I hope this can sort of help, I didn't really want to exactly answer your questions but more of show you (from my understanding) what the point of all of this is. It's really just lex gets the tokens and then you have to specify what form those tokens come in, otherwise they make no sense (because lex doesn't care about form, just about matching regex's). Hopefully someone else can chime in more about this.
 
Thanks so much! This is very helpful! :D


Will get to it and be back. Thanks again!
 
Back with some answers, for all those interested.



The flex code for b reads:
Code:
%{
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char *name;
char *yytext;
int myvar;
%}              
 /*these are just declarations*/

%%
[A-E]* {myvar=yyleng; return yyleng;}      
%%    

/*here we manipulate input. Whenever flex comes across an expression of the form
 "nothing, A to E, A to E,..." it saves the number of times
 a letter from A to E occurs*/



int main() {
yylex();
name=yytext;
if(myvar==0) {printf("wrong input\n");}
if(myvar==1) {printf(":Angle\n");
printf("%s :Angle Name",name);}
if(myvar==2) {printf(": Line Segment\n");
printf("%s : Line Segment name",name);}
if(myvar==3) {printf(": Triangle\n");
printf("%s : Triangle Name",name);}
return 0;
}

 
/*this is the main routine, where depending on the number of instances flex has encountered a letter from A to E, it returns the relevant assignment*/



Note that, given the input TRIANGLE ABF, flex with recognize this as a line segment!
Here's where the finite automaton associated with this rule becomes non-deterministic.
More on this to follow.
 
Back
Top