I would try to solve this problem using a totally different technique
involving lexer start states. It's a little tricky, but here's the gist of
it.
Define a new state in the lexer:
states = (
('ASM','inclusive'),
)
Modify the lexer rule that looks for identifiers/keywords to flip
the state when the word 'asm' is encountered.
def t_ID(t):
...
if t.value == 'asm':
t.lexer.begin('ASM')
Write a lexer rule for the opening { in ASM state. Have this rule
collect the whole { } block and return it as a special ASMBLOCK
token. Have this rule flip the state back to 'INITIAL' when done.
def t_ASMSTART_LBRACE(t):
r'\{'
end = t.lexer.lexdata.find('}',t.lexer.lexpos)
t.value = t.lexer.lexdata[t.lexer.lexpos-1:end+1]
t.type = 'ASMBLOCK'
t.lexer.lexpos = end+1
t.lexer.begin('INITIAL')
return t
In your parser, replace all of the asm_compound_statement
rules with just the ASMBLOCK token.
Essentially the idea here is that whenever the "asm" keyword
is encountered, the lexer will flip to a state that handles the
next { } block differently--returning it as a different kind of
token.
The "Conditional Lexing and Start Conditions" section of the
PLY documentation has more details about lexer states as well
as a more complex example of capturing curly braces where you
might have to worry about { } characters being nested, appearing
in string literals, and in comments.
Hope this helps.
Cheers,
Dave
On Sun 18/01/09 5:43 AM , odin95 [email protected] sent:
>
> In C code I can have assembly code with this syntax :
>
> asm function_declaration
> {
> assembly instruction
> ...
> assembly instruction
> }
>
> In the file cparse.py I make a declaration of "asm
> function_declaration", but I do not see how to define the rule to
> take all the lines between '{' and '}' to be treated without having to
> declare all assembly code op.
>
> The "function_declaration" is standard C function_declaration,
> with orwithout parameters and return or not.
>
>
> I use PLY 2.5
>
> extract of my code in cparse.py
>
> # asm function declaration
> def p_asm_function_definition_1(t):
> 'asm_function_definition : ASM declaration_specifiers declarator
> asm_compound_statement'
>
> def p_asm_function_definition_2(t):
> 'asm_function_definition : ASM declarator declaration_list
> asm_compound_statement'
>
> def p_asm_function_definition_3(t):
> 'asm_function_definition : ASM declarator asm_compound_statement'
>
> def p_asm_function_definition_4(t):
> 'asm_function_definition : declaration_specifiers declarator
> asm_compound_statement'
>
> # asm compound statement
> def p_asm_compound_statement(t):
> 'asm_compound_statement : LBRACE asm_statement_list RBRACE'
>
> # asm_statement-list:
> def p_asm_statement_list_1(t):
> 'asm_statement_list : asm_statement'
>
> def p_asm_statement_list_2(t):
> 'asm_statement_list : asm_statement_list asm_statement'
>
> # asm_statement
> def p_asm_statement(t):
> 'asm_statement : asm_code asm_operande'
>
> # asm_code
> def p_asm_code(t):
> 'asm_code : all asm code op ????
>
> # asm_operande
> def p_asm_operande(t):
> 'asm_operande : all operande possible ????
>
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups
> "ply-hack" group.To post to this group, send email to ply
> [email protected] unsubscribe from this group, send email to ply-hack+
> [email protected] more options, visit this group at
> http://groups.google.com/group/ply-hack?hl=en-~----------~----~----~----
~------~----~------~--~---
>
>
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---