help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: please help me with this


From: Philip Herron
Subject: Re: please help me with this
Date: Sat, 19 Dec 2009 20:26:36 +0000

On Fri, 2009-12-18 at 01:58 -0500, Tejas Kajarekar wrote:
> Hello,
> My name is Tejas. I am trying to use Flex and Bison to parse (and convert)
> one file to another format. Currently, I am not bothered about conversion,
> just the parsing.
> I have never used either tools, and what my problem is, may be trivial.
> (Worth laughing)
> Here is my problem:
> I am going to attach my .l and .y files and .txt file which is my input.
> Here I will just include the tokenIZING part of .l and grammar of .y.
> This is part of my flex which returns tokens to yyparse:-
> "graph"        {    return GRAPH;    }
> "{"               {    return '{';             }
> "}"               {    return '}';             }
> "init"            {    yylval= yytext;
>                         return INIT;         }
> {id}              {    yylval = yytext;
>                         return ID;            }
>  where id = [a-zA-Z_][a-zA-z_0-9]*
> 
> This is my bison grammar:-
> %%
> input:                    /* empty */
>                             | GRAPH '{' graphitemset '}'    { printf("GRAPH
> FOUND");      }
>                             | error
> 
> graphitemset:        responsivestate            { printf("RESPONSIVE
> STATE");    }
> 
> responsivestate:    INIT statename '{' '}'        { printf("INIT %s",$2);
>      }
> 
> statename:            /* empty */
>                             | ID
> 
> %%
> 
> After running it,
> C:\>gnu\bin\flex -olex.yy.c -i s.l
> C:\>gnu\bin\bison -d s.y
> C:\>gcc -ose s.tab.c lex.yy.c -ly -lfl
> C:\>se < s.txt
> Now if my s.txt = *graph{init{}}*
> then I get output *INIT (null)RESPONSIVE STATEGRAPH FOUND.*
> 
> However, if I change s.txt to *graph{initnabc{}}*
> then I get *se: syntax error line 0*
> i.e., it only recognizes state names which are empty, otherwise, there is
> some problem with ID.
> 
> I want it to recognoze graph, init as well as the state name.
> I think ID definition conflicts with "graph", or "init".
> It also generates a warning: No new line at end of file for .l file.
> Please help me with this problem.
> __________________________________________________________________________
> Also, simlar kind of problem, but with very simple files:
> "graph"        {    return GRAPH;    }
> {id}        {    yylval = yytext;
>             return ID;    }
> for tokens
> 
> 
> input:            GRAPH { printf("GRAPH FOUND");      }
>             | GRAPH ID { printf("GRAPH + ID");    }
> the grammar
> 
> with input graph, its executes to* GRAPH FOUND;*
> with input graphgraph, it gives syntax error; and not GRAPH FOUND twice.,
> nor GRAPH + ID for something like graphbhg.
> 
> Do we have to run the tokenizing routing in some loop like while not EOF,
> etc.?
> 
> I have no idea what to do to get the desired output.
> This may sound very funny, but as I said, I have no experience with these
> two tools.
> I will really appreciate your help.
> The actual input will be quite large, I am just experimenting with
> graph{init{}}, and having problems with that
> Also, sorry for the long email.
> 
> thanking you in advance,
> tejasSK
> _______________________________________________
> address@hidden http://lists.gnu.org/mailman/listinfo/help-bison

Hey

I think i see your problem, but not quite sure which attachments are
relevant.

Is the s.txt the input data i take it: ``graph { init { } }''

Anyway's not totaly sure on your syntax. But if you want to build a
lexer and parser for this you need to think about your syntax a little
more as in know what is syntax/delimiters/operators and what are
keywords and identifiers.

So from here you only have 3 things to worry about 'lexing' which are
'{', '}' keywords 'graph' 'init' and IDENTIFIERS

So i take it from your s.y grammar you want the syntax to be something
like:

'graph { init <statename> { <parameters> } }'

Well first i see you wanted it to look like 'graph{ initnabc { } }',
your lexer cant split out the 'init' and 'abc' since the regex for that
token will simply return an IDENTIFIER, since it see init<more chars> so
it can't be the INIT keyword it has to be an identifier. So you language
MUST be something like ``'graph { init abcdefg { } }' unless you want to
treat the 'initabc' as an IDENTIFIER and do the lexing your self.

So this leaves making your lexer look like this:

"graph"         {       return GRAPH;   }
"{"             {       return '{';     }
"}"             {       return '}';     }
"init"          {       return INIT;    }
{id}            { yylval.string= strdup(yytext);
                 return ID;      }

Note i have started using 'yylval.<>' you should get in the habit of
using a <%union> in bison since it makes type handling much easier since
YYSTYPE is default int i IIRC and you can do #define YYSTYPE const char*
or somthing but its simply not dynamic enough.

So your lexer if it is the 's.l' needs a simply edit and make sure you
include the header generated by bison in a sec.

In your parser you need to add in your:

%union{ const char* string; <other types you wish to return> }

Then your grammar:

%token GRAPH
%token INIT
%token<string> ID

/* You can also add in %type<union ident type> rule for semantics which
return values using $$=<foo>; */

%start graph

%%

graph: /* empty */
     | GRAPH '{' graphparameters '}'
     | error
     ;

graphparameters: 
     INIT stateid '{' '}'
     ;

stateid: /* empty */
     | ID 
     ;

%%

And then you can add in what you want so your grammar was right. But
with a little cleanup your better ready to handle more semantics easier
using %union :). Remember think about what your syntax means is really
important. I learnt that the hard way ;).

Then you could start handling n length parameters for your state with
grammar like:

%type<string> stateid
%type<paramlist> parameters
%type<symbol> item

%%

graph: /* empty */
     | GRAPH '{' graphparameters '}'
     | error
     ;

graphparameters: 
     INIT stateid '{' parameters '}'
     ;

stateid: /* empty */
     | ID { $$= $1; }
     ;

parameters:
       parameters item
     | item
     ;

item: INTEGER
     | ...
     ;

Hope this helps.

--Phil

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]