Create tokens for the indentation/curly braces and the 'if' keyword
and then grammar rules that handle sequences of those tokens.  I can't
go into too much more than that.  The examples included with PLY cover
quite a bit, really.  Have a look at
http://www.juanjoconti.com.ar/files/python/ply-examples/GardenSnake/GardenSnake.py.html
for a partial implementation of Python called GardenSnake (including
the "if" statement).

On Tue, Feb 10, 2009 at 3:33 PM, matio <[email protected]> wrote:
>
> I meant how would I create an "if" statement, or anything that would
> need to be indented/in curly braces?
> Would I have to completely re-write the code or is there a way just to
> add it in as it is now (I've looked at the examples but it didn't seem
> to work)?
>
> On Feb 9, 6:21 pm, matio <[email protected]> wrote:
>> How would I make something like an if?
>>
>> On Feb 8, 1:21 pm, David Beazley <[email protected]> wrote:
>>
>> > Either propagate values or don't print the result of the parse.
>>
>> > -Dave
>>
>> > On Sun 08/02/09  8:04 AM , matio [email protected] sent:
>>
>> > > how do I fix it?
>>
>> > > On Feb 8, 12:58 pm, David Beazley <[email protected]
>> > > om> wrote:> The calculator is printing the result of the
>> > > parse.  It's printing> None because you're not propagating
>> > > values.>
>> > > > -Dave
>>
>> > > > On Sun 08/02/09  7:50 AM , matio ma...@hot
>> > > mail.co.uk sent:>
>>
>> > > > > what does that mean?
>>
>> > > > > On Feb 8, 12:18 pm, David Beazley
>> > > <[email protected]> > om> wrote:> You're not propagating
>> > > values correctly by> > assigning to p[0].> For example, in the
>> > > p_statement_expr()> > rule.>
>> > > > > > -Dave
>>
>> > > > > > On Sun 08/02/09  6:20 AM , matio
>> > > ma...@hot> > mail.co.uk sent:>
>>
>> > > > > > > I've been using ply & have
>> > > extended the> > example slightly:>
>> > > > > > > [code]
>> > > > > > > <code>
>> > > > > > > import ply.lex as lex
>>
>> > > > > > > reserved = {'print':'PRINT',
>> > > 'quit' :> > 'QUIT'}>
>> > > > > > > # List of token names.
>> > > This is> > always required> > tokens =
>> > > [> > > > 'NUMBER',
>> > > > > > > 'NAME',
>> > > > > > > 'PLUS',
>> > > > > > > 'MINUS',
>> > > > > > > 'TIMES',
>> > > > > > > 'DIVIDE',
>> > > > > > > 'LPAREN',
>> > > > > > > 'RPAREN',
>> > > > > > > 'EQUALS',
>> > > > > > > 'STRING',
>> > > > > > > ] +
>> > > list(reserved.values())>
>> > > > > > > # Regular expression rules for
>> > > simple> > tokens> > t_PLUS    =
>> > > r'\+'> > > > t_MINUS   = r'-'
>> > > > > > > t_TIMES   = r'\*'
>> > > > > > > t_DIVIDE  = r'/'
>> > > > > > > t_LPAREN  = r'\('
>> > > > > > > t_RPAREN  = r'\)'
>> > > > > > > t_EQUALS = r'='
>>
>> > > > > > > def t_NAME(t):
>>
>> > > r'[a-zA-Z_][a-zA-Z_0-9]*'> > > > t.type =
>> > > reserved.get(t.value,'NAME')> >    # Check for reserved> >
>> > > words> > > > return t
>>
>> > > > > > > # A regular expression rule with
>> > > some> > action code> > def
>> > > t_NUMBER(t):> > > > r'\d+'
>> > > > > > > t.value = int(t.value)
>> > > > > > > return t
>>
>> > > > > > > # Define a rule so we can track
>> > > line> > numbers> > def
>> > > t_newline(t):> > > > r'\n+'
>> > > > > > > t.lexer.lineno +=
>> > > len(t.value)>
>> > > > > > > def t_STRING(t):
>>
>> > > > > r'\"([^\\"]|(\\.))*\"'>
>> > > > escaped = 0> > > > str = t.value[1:-1]
>> > > > > > > new_str = ""
>> > > > > > > for i in range(0,
>> > > len(str)):> > > > c = str[i]
>> > > > > > > if escaped:
>> > > > > > > if c == "n":
>> > > > > > > c = "\n"
>> > > > > > > elif c ==
>> > > "t":> > > > c = "\t"
>> > > > > > > new_str += c
>> > > > > > > escaped = 0
>> > > > > > > else:
>> > > > > > > if c ==
>> > > "\\":> > > > escaped = 1
>> > > > > > > else:
>> > > > > > > new_str += c
>> > > > > > > t.value = new_str
>> > > > > > > return t
>>
>> > > > > > > # A string containing ignored
>> > > characters> > (spaces and tabs)> > t_ignore  =
>> > > ' \t'>
>> > > > > > > # Error handling rule
>> > > > > > > def t_error(t):
>> > > > > > > print "Illegal character
>> > > '%s'" %> > t.value[0]> >
>> > > t.lexer.skip(1)>
>> > > > > > > # Build the lexer
>> > > > > > > lexer = lex.lex()
>> > > > > > > [/code]
>> > > > > > > </code>
>>
>> > > > > > > &
>>
>> > > > > > > [code]
>> > > > > > > <code>
>> > > > > > > import ply.yacc as
>> > > yacc> > > > import sys
>>
>> > > > > > > # Get the token map from the
>> > > lexer.> >  This is required.> > from
>> > > calclex import tokens>
>> > > > > > > names = {}
>> > > > > > > def
>> > > p_statement_assign(t):> > > > 'statement : NAME
>> > > EQUALS> > expression'> > names[t[1]] =
>> > > t[3]>
>> > > > > > > def
>> > > p_statement_expr(t):> > > > 'statement :
>> > > expression'> > > > if t[1] != None:
>> > > > > > > print t[1]
>> > > > > > > else:
>> > > > > > > pass
>>
>> > > > > > > def
>> > > p_expression_name(t):> > > > 'expression : NAME'
>> > > > > > > try:
>> > > > > > > t[0] = names[t[1]]
>> > > > > > > except LookupError:
>> > > > > > > print "Undefined name
>> > > '%s'" %> > t[1]> > t[0] = 0
>>
>> > > > > > > def
>> > > p_expression_plus(p):> > > > 'expression : expression
>> > > PLUS> > term'> > p[0] = p[1] +
>> > > p[3]>
>> > > > > > > def
>> > > p_expression_minus(p):> > > > 'expression : expression
>> > > MINUS> > term'> > p[0] = p[1] -
>> > > p[3]>
>> > > > > > > def
>> > > p_expression_term(p):> > > > 'expression : term'
>> > > > > > > p[0] = p[1]
>>
>> > > > > > > def p_term_times(p):
>> > > > > > > 'term : term TIMES
>> > > factor'> > > > p[0] = p[1] * p[3]
>>
>> > > > > > > def p_term_div(p):
>> > > > > > > 'term : term DIVIDE
>> > > factor'> > > > p[0] = p[1] / p[3]
>>
>> > > > > > > def p_term_factor(p):
>> > > > > > > 'term : factor'
>> > > > > > > p[0] = p[1]
>>
>> > > > > > > def p_factor_num(p):
>> > > > > > > 'factor : NUMBER'
>> > > > > > > p[0] = p[1]
>>
>> > > > > > > def p_factor_expr(p):
>> > > > > > > 'factor : LPAREN
>> > > expression> > RPAREN'> > p[0] = p[2]
>>
>> > > > > > > def p_print(p):
>> > > > > > > '''expression : PRINT LPAREN
>> > > STRING> > RPAREN> > | PRINT LPAREN
>> > > expression> > RPAREN'''> > p[0] = p[3]
>>
>> > > > > > > def p_quit(p):
>> > > > > > > 'expression : QUIT'
>> > > > > > > sys.exit()
>>
>> > > > > > > # Error rule for syntax
>> > > errors> > > > def p_error(p):
>> > > > > > > print "Syntax error
>> > > in> > input!">
>> > > > > > > # Build the parser
>> > > > > > > parser = yacc.yacc()
>>
>> > > > > > > if len(sys.argv) == 1:
>> > > > > > > while True:
>> > > > > > > try:
>> > > > > > > s = raw_input('calc >
>> > > ')> > > > except EOFError:
>> > > > > > > break
>> > > > > > > if not s: continue
>> > > > > > > result =
>> > > parser.parse(s)> > > > print result
>>
>> > > > > > > elif sys.argv[1] ==
>> > > > > "-f":> > f =
>> > > open(sys.argv[2],> > "r")> > s =
>> > > f.readlines()> > > > for line in s:
>> > > > > > > result =
>> > > parser.parse(line)> > > > if result != None:
>> > > > > > > print "result !=
>> > > none"> > > > print result
>> > > > > > > [/code]
>> > > > > > > </code>
>> > > > > > > When I run
>> > > print("hello") it> > says:> > hello
>> > > > > > > none
>> > > > > > > calc>
>>
>> > > > > > > What's wrong?
>> > > > > > > I also posted it 
>> > > > > > > athttp://ubuntuforums.org/showthread.php?p=6698006#post6698006&;
>> > > amp;> > gt;>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ply-hack" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/ply-hack?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to