Note that there are some explanatory texts on larger screens.

plurals
  1. PODebugging Pyparsing Grammar
    text
    copied!<p>I'm building a parser for an imaginary programming language called C-- (not the actual C-- language). I've gotten to the stage where I need to translate the language's grammar into something Pyparsing can accept. Unfortunatly when I come to parse my input string (which is correct and should not cause Pyparsing to error) it's not parsing correctly. I fear this is due to errors in my grammar, but as I'm starting Pyparsing for the first time, I can't seem to see where I'm going wrong.</p> <p>I've uploaded the grammar that I'm translating from <a href="http://www.box.net/shared/5a8lc7jz0p" rel="noreferrer">here</a> for people to have a read through.</p> <p><strong>EDIT:</strong> Updated with the advice from Paul.</p> <p>This is the grammer I've currently got (the two top lines of Syntax definition are terribly bad of me I know):</p> <pre><code># Lexical structure definition ifS = Keyword('if') elseS = Keyword('else') whileS = Keyword('while') returnS = Keyword('return') intVar = Keyword('int') voidKeyword = Keyword('void') sumdiff = Literal('+') | Literal('-') prodquot = Literal('*') | Literal('/') relation = Literal('&lt;=') | Literal('&lt;') | Literal('==') | \ Literal('!=') | Literal('&gt;') | Literal('=&gt;') lbrace = Literal('{') rbrace = Literal('}') lparn = Literal('(') rparn = Literal(')') semi = Literal(';') comma = Literal(',') number = Word(nums) identifier = Word(alphas, alphanums) # Syntax definition term = '' statement = '' variable = intVar + identifier + semi locals = ZeroOrMore(variable) expr = term | OneOrMore(Group(sumdiff + term)) args = ZeroOrMore(OneOrMore(Group(expr + comma)) | expr) funccall = Group(identifier + lparn + args + rparn) factor = Group(lparn + expr + rparn) | identifier | funccall | number term = factor | OneOrMore(prodquot + factor) cond = Group(lparn + expr + relation + expr + rparn) returnState = Group(returnS + semi) | Combine(returnS + expr + semi) assignment = Group(identifier + '=' + expr + semi) proccall = Group(identifier + lparn + args + rparn + semi) block = Group(lbrace + locals + statement + rbrace) iteration = Group(whileS + cond + block) selection = Group(ifS + cond + block) | Group(ifS + cond + block + elseS + block) statement = OneOrMore(proccall | assignment | selection | iteration | returnState) param = Group(intVar + identifier) paramlist = OneOrMore(Combine(param + comma)) | param params = paramlist | voidKeyword procedure = Group(voidKeyword + identifier + lparn + params + rparn + block) function = Group(intVar + identifier + lparn + params + rparn + block) declaration = variable | function | procedure program = OneOrMore(declaration) </code></pre> <p>I'd like to know if there are any mistakes I've made in translating the grammar across and what improvements I could do to make it simplified whilst adhering to the grammar I've been given.</p> <p><strong>EDIT 2:</strong> Updated to include the new error.</p> <p>Here is the input string I am parsing:</p> <pre><code>int larger ( int first , int second ) { if ( first &gt; second ) { return first ; } else { return second ; } } void main ( void ) { int count ; int sum ; int max ; int x ; x = input ( ) ; max = x ; sum = 0 ; count = 0 ; while ( x != 0 ) { count = count + 1 ; sum = sum + x ; max = larger ( max , x ) ; x = input ( ) ; } output ( count ) ; output ( sum ) ; output ( max ) ; } </code></pre> <p>And this is the error message I get when running my program from Terminal:</p> <pre><code>/Users/Joe/Documents/Eclipse Projects/Parser/src/pyparsing.py:1156: SyntaxWarning: null string passed to Literal; use Empty() instead other = Literal( other ) /Users/Joe/Documents/Eclipse Projects/Parser/src/pyparsing.py:1258: SyntaxWarning: null string passed to Literal; use Empty() instead other = Literal( other ) Expected ")" (at char 30), (line:6, col:26) None </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload