I'm trying to figure out the grammar for the following syntax.
foreach
where x = 1 when some_variable = true
where x = 2 when some_variable = false
where y = 0
print z // Main block
when none // optional
print 'not found' // Exception block
endfor
My grammar looks like:
foreach_stmt : 'for' 'each' where_opt* blockstmt* whennone_opt? 'endfor'
;
where_opt : 'where' cond_clause
;
cond_clause : test when_opt*
;
when_opt : 'when' test
;
whennone_opt : 'when' 'none' blockstmt*
;
test : or_test
;
// further rules omitted
But when the main block is blank, for example
foreach
where x = 1
// main block is blank, do nothing
when none
print 'none'
endfor
In this case my grammar considers "when none" is a cond_clause to "where x = 1" which is not what I'm expecting.
Also consider the following case:
foreach
where x = 1 when none = 2
print 'none'
// exceptional block is blank
endfor
Where the "none" can be a variable, and "none = 2" should match the "test" rule so it's part of "where...when...".
However when "none" is not in an expression statement, I want "when none" match the "foreach" rather than the previous "where". How can I modify my grammar to do this?
Sorry this title sucks but I don't know how to describe the problem in a few words. Any help would be greatly appreciated.
none
to be a keyword and an identifier at the same time? You're making it hard for yourself to parse such sources. I'm also wondering whatblockstmt
could be: could you post your entire grammar? – Bart Kiersnone
is a valid variable name theoretically, I think it's also acceptable to ignore the case since one will hardly do such crazy thing I guess... The entire grammar is a bit large and most rules are not related to this problem. I'll try to write a minimal grammar. – Shaung