help-bison
[Top][All Lists]
Advanced

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

Re: Recognize grammar prefix.


From: lfinsto1
Subject: Re: Recognize grammar prefix.
Date: Tue, 15 Jan 2008 10:45:53 +0100 (CET)
User-agent: SquirrelMail/1.4.9a

Please "cc" any replies to the help-bison list.

> The problem is that I must call the parser in another program. My program
> read some strings and I would like pass these strings to bison. So, I have
> a
> grammar like this:
>
> S1 = lemon S2 | apricot S2
> S2 = kiwi orange | S3
> S3 = banana S3 | strawberry S1
>
> At certain moment I have lemon (and ONLY lemon!!), after banana, after
> again
> strawberry. I must validate each "fruit" from time to time. If I call
> bison
> normally I can insert like input: first "lemon" (the parser don't return
> syntax error but request another input...), after banana, etc...Ok?
>
> So how I can call bison in another program and passing strings to
> validate?
> To reach my goal I must run bison normally and "inject" my string like an
> input and check if bison return a syntax error or not...How I can perform
> this "injection"?

Make rules `banana lemon END', `strawberry lemon END', and any other valid
combination of "fruit" strings, and "fake" the `END' token.  They must
reduce to the start symbol of your grammar for `yyparse' to return
successfully.

I have described the technique of "faking" tokens many times on this list.
 It simply means to arrange to have `yylex' pass a token to `yyparse' that
is not generated from the input.  I use a stack that is available to
`yylex'.  A  token can be placed on the stack in an action in your
grammar, but it could also be placed on it from somewhere else in your
program.  `yylex' first examines the stack.  If there's a token on it, it
passes that token to `yyparse' and returns immediately.  Otherwise, it
reads from its input.

That's the gist of it.  For more information, please look through my old
postings.

This isn't necessarily the only way of solving the problem.

Laurence Finston






reply via email to

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