I had to import string in parser.py to get it to work.
I then have run into another problem.
Stack trace as follows.
-----------------------------------------
>python Main.py
Token: select
Token: 1
Token: from
Reduction: Value
Reduction: Factor
Reduction: Term
Reduction: ValueExpression
Reduction: PredicateExp
Reduction: Not Exp
Reduction: And Exp
Reduction: Or Exp
Reduction: Expression
Reduction: SelectSublist
Reduction: SelectMember
Reduction: SelectList
Traceback (most recent call last):
File "Main.py", line 48, in ?
main()
File "Main.py", line 44, in main
result = p.parse()
File "C:\Python23\Lib\site-packages\pygold\Parser.py", line 201, in parse
response = self.parseStep()
File "C:\Python23\Lib\site-packages\pygold\Parser.py", line 293, in parseStep
result = self.parseToken(token)
File "C:\Python23\Lib\site-packages\pygold\Parser.py", line 517, in parseToken
result = self.reduce(self._rules[action.value])
File "C:\Python23\Lib\site-packages\pygold\Parser.py", line 557, in reduce
index = self._tempstack.peekToken().state
File "C:\Python23\Lib\site-packages\pygold\TokenStack.py", line 41, in peekTok
en
return self.items[-1]
IndexError: list index out of range
-----------------------------------------
Python
======
-----------------------------------------
def onToken(parser, token):
print 'Token: ' + token.data
def onReduction(parser, reduction, tokens):
print 'Reduction: ', reduction.name
def main():
global input
## Yes, I'm using windows-1250 codepage
## for this moment Unicode is *NOT* supported so you
## have to specify codepage, use 'utf-8' if you're not
## sure which you should use
p = pygold.Parser('ASQL.cgt', 'utf-8')
p.openFile('Simple.sql')
p.onReduction = onReduction
p.onToken = onToken
input = ''
result = p.parse()
p.close()
if __name__ == '__main__':
main()
--------------------------------------
--------------------------------------
! -----------------------------------------------------------------------------------
! ASQL Grammar
! -----------------------------------------------------------------------------------
"Name" = 'ASQL'
"Version" = '0'
"About" = 'AQSL Grammar'
! =============================================================================
! Comments
! =============================================================================
Comment Start = '/*'
Comment End = '*/'
"Start Symbol" = <Statements>
! =============================================================================
! Terminals
! =============================================================================
{String Ch 1} = {Printable} - ["]
{String Ch 2} = {Printable} - ['']
{Id Ch Standard} = {Alphanumeric} + [_] + [%] - [/]
{Id Ch Extended} = {Printable} - ['['] - [']'] - [/]
StringLiteral = '"'{String Ch 1}*'"' | ''{String Ch 2}*''
IntegerLiteral = {Digit}+
RealLiteral = {Digit}+'.'{Digit}+
SimpleId = {Digit}*{Letter}{Id Ch Standard}*
{Var Ch} = {Digit} + {Letter} + [#] + [_] + [%]
VariableName = [@]{Var Ch}+
{Var Literal Ch} = {Printable} - [}] - [{]
VariableLiteral = '{' {Var Literal Ch}* '}'
! =============================================================================
! Key words
! =============================================================================
<Id> ::= SimpleId
| SimpleId '.' <Id>
| SimpleId '.' '*'
| SimpleId '[' IntegerLiteral ']'
| SimpleId '[' '%' ']'
| group
<Variable> ::= VariableName
| VariableName VariableLiteral
<Statements> ::= <Statement> <Statements>
| <Statement>
! =============================================================================
! Statement
! =============================================================================
<Statement> ::= <SelectStm>
| <SelectStm> UNION <Statement>
! =============================================================================
! Select Statement
! =============================================================================
<SelectStm> ::= SELECT <SelectList> <Into Clause> <From Clause> <Where Clause> <Group Clause> <Having Clause> <Order Clause>
<SelectList> ::= <SelectMember> ',' <SelectList>
| <SelectMember>
<SelectMember> ::= <SetQuantifier> <SelectSublist>
| <SelectSublist>
<SetQuantifier> ::= DISTINCT
|
<SelectSublist> ::= '*'
| <Id> '.' '*'
| <Expression>
| <Expression> StringLiteral
! =============================================================================
<Into Clause> ::= INTO SimpleId
|
! =============================================================================
<From Clause> ::= FROM <From Id List>
<From Id List> ::= <From Id Member> ',' <From Id List>
| <From Id Member>
<From Id Member> ::= <Id>
| <Id> SimpleId
| <Id> SimpleId 'Fill'
! =============================================================================
<Where Clause> ::= WHERE <Expression>
|
! =============================================================================
<Group Clause> ::= GROUP BY <Group Id List>
|
<Group Id List> ::= <Group Id Member> ',' <Group Id List>
| <Group Id Member>
<Group Id Member> ::= <Id>
| IntegerLiteral
! =============================================================================
<Order Clause> ::= ORDER BY <Order Id List>
|
<Order Id List> ::= <Order Id Member> ',' <Order Id List>
| <Order Id Member>
<Order Id Member> ::= <Id> <Order Type>
| IntegerLiteral <Order Type>
<Order Type> ::= ASC
| DESC
|
! =============================================================================
<Having Clause> ::= HAVING <Expression>
|
! =============================================================================
! Expressions
! =============================================================================
<Expression> ::= <Or Exp>
| <TertiaryExpression>
<TertiaryExpression> ::= <Expression> '?' <Expression> ':' <Expression>
<Or Exp> ::= <And Exp> OR <Expression>
| <And Exp>
<And Exp> ::= <Not Exp> AND <And Exp>
| <Not Exp>
<Not Exp> ::= NOT <PredicateExp>
| <PredicateExp>
<PredicateExp> ::= <ValueExpression> BETWEEN <ValueExpression> AND <ValueExpression>
| <ValueExpression> NOT BETWEEN <ValueExpression> AND <ValueExpression>
| <ValueExpression> NOT LIKE StringLiteral
| <ValueExpression> NOT LIKE <Variable>
| <ValueExpression> LIKE StringLiteral
| <ValueExpression> LIKE <Variable>
| <ValueExpression> RE_LIKE StringLiteral
| <ValueExpression> RE_LIKE <Variable>
| <ValueExpression> NOT RE_LIKE StringLiteral
| <ValueExpression> NOT RE_LIKE <Variable>
| <ValueExpression> NOT IN <Tuple>
| <ValueExpression> IN <Tuple>
| <ValueExpression> '=' <ValueExpression>
| <ValueExpression> '*=' <ValueExpression>
| <ValueExpression> '=*' <ValueExpression>
| <ValueExpression> '<>' <ValueExpression>
| <ValueExpression> '~=' <ValueExpression>
| <ValueExpression> '>' <ValueExpression>
| <ValueExpression> '>=' <ValueExpression>
| <ValueExpression> '<' <ValueExpression>
| <ValueExpression> '<=' <ValueExpression>
| <ValueExpression>
<Tuple> ::= '(' <Expr List> ')'
<Expr List> ::= <Expression> ',' <Expr List>
| ',' <Expr List>
| <Expression>
| '*'
|
<ValueExpression> ::= <Term>
| <ValueExpression> '+' <Term>
| <ValueExpression> '-' <Term>
<Term> ::= <Factor>
| <Term> '*' <Factor>
| <Term> '/' <Factor>
<Factor> ::= <Value>
| '-' <Value>
| '+' <Value>
<Value> ::= <Id>
| StringLiteral
| IntegerLiteral
| RealLiteral
| '(' <Expression> ')'
| <Variable>
| <Function>
<Function> ::= SimpleId '(' <Expr List> ')'
--------------------------------------
--------------------------------------
select
1
from
Table t
--------------------------------------
Logged In: YES
user_id=2092509
Originator: NO
The bug is in the handling of empty rules #1967806 The solution (unchecked as far as I can see is to change the funciton popTokensInto)
TokenStack::popTokensInto <- (TokenStack.py)
def popTokensInto(self, reduction, count):
''' Pops 'count' tokens from the stack and appends them
to specified 'reduction'.
Returns None.
'''
if count == 0:
return
l = self.items[-count:]
del self.items[-count:]
for i in l:
reduction.append(i)