On Thu, Feb 28, 2008 at 03:16:40PM +0100, Ilyes Gouta wrote: > Hi! > > I'm writing a small grammar for a very simplified C language. My goal > is to produce an AST once a script file is parsed by the flex/bison > tools. I got almost everything working nicely, except for the C style > for loops. > > Let's assume that we have a rule like this: > > unary: > const > | id > ; > > expr: > unary > | expr + expr > | expr - expr > | expr * expr > | expr / expr > ; > > assignment: > id '=' expr ';' > ; > > for_stmt: > for (assignment; expr; assignment) block > ; > > My problem is that I'd like to support loops with empty assignment and > expr parts (i.e, always true loops and the likes) such as: > > int a, b, c; > > for (;;) { > c = a + b; > } > > but I couldn't find a way to identify an empty assignment (or > expression) and to define the action associated (to push/construct a > new node within my AST). How one would modify the grammar to take into > account such a scenario? > > Any ideas?
Add a full expression, fexpr, that can either be nothing, or an expression. Same with the assignment. However, an assignment is an expression, so I think you shouldn't separate them out. Bob Rossi _______________________________________________ help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison