[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
how to create additional to CST attribute in AST ?
Hello,
I want to add attribute into AST class, which have no
relation to grammar. This attribute will be filled
during semantic analyses stage.
For example, i want to add "error" attribute to AST
classes. Or I might wnat to add "id" attribute for
some classes.
I can, of cource, modify generated AST classes. But I
hope there is workaround, which allows to avoid such
step.
As example, consider following valid grammar.
/*-----------------------------------*/
Productions
/*-----------------------------------*/
lib_elem {-> lib_elem} = tk_program
[name]:tk_identifier tk_endprogram {-> New
lib_elem.program(name)}
;
/*-----------------------------------------*/
Abstract Syntax Tree
/*-----------------------------------------*/
lib_elem = {program} [name]:tk_identifier
;
I would like to add additional field "id" which can
store "tk_identifier" to production lib_elem in AST:
lib_elem = {program} [name]:tk_identifier
[id]:tk_identifier
;
Is it possible to modify CST production lib_elem in
such way, that grammar would be valid ? Constructions
like
lib_elem {-> lib_elem} = tk_program
[name]:tk_identifier tk_endprogram {-> New
lib_elem.program(name, Null)}
;
are not working for me ... .
I attached valid and "would like to be" grammars.
Thank you,
Viktor
____________________________________________________________________________________
Sponsored Link
Mortgage rates near 39yr lows. $420k for $1,399/mo.
Calculate new payment!
www.LowerMyBills.com/lrePackage parsers.aspect;
/*--------------------------------------------------*/
Helpers
/*--------------------------------------------------*/
hlp_program = 'PROGRAM';
hlp_endprogram = 'END_PROGRAM';
hlp_tab = 9;
hlp_cr = 13;
hlp_lf = 10;
hlp_eol = (hlp_cr hlp_lf) | hlp_cr | hlp_lf;
hlp_space = ' ';
hlp_letter = ['A' .. 'Z'] | ['a' .. 'z'];
hlp_digit = ['0' .. '9'];
hlp_id_head = (hlp_letter | ('_' (hlp_letter | hlp_digit)));
hlp_id_tail = (('_')? (hlp_letter | hlp_digit))*;
hlp_identifier = hlp_id_head hlp_id_tail;
/*----------------------------------------------------*/
Tokens
/*----------------------------------------------------*/
tk_program = hlp_program;
tk_endprogram = hlp_endprogram;
tk_ignore = hlp_tab | hlp_eol | hlp_space;
tk_identifier = hlp_identifier;
/*-------------------------------------------------*/
Ignored Tokens
/*-------------------------------------------------*/
tk_ignore;
/*---------------------------------------------------*/
Productions
/*----------------------------------------------------*/
aspect {-> aspect} =
lib_elem* {-> New aspect([lib_elem.lib_elem])}
;
lib_elem {-> lib_elem} = tk_program [name]:tk_identifier tk_endprogram {-> New lib_elem.program(name)}
;
/*-----------------------------------------*/
Abstract Syntax Tree
/*-----------------------------------------*/
aspect = [lib_elems]:lib_elem*
;
lib_elem = {program} [name]:tk_identifier
;Package parsers.aspect;
/*--------------------------------------------------*/
Helpers
/*--------------------------------------------------*/
hlp_program = 'PROGRAM';
hlp_endprogram = 'END_PROGRAM';
hlp_tab = 9;
hlp_cr = 13;
hlp_lf = 10;
hlp_eol = (hlp_cr hlp_lf) | hlp_cr | hlp_lf;
hlp_space = ' ';
hlp_letter = ['A' .. 'Z'] | ['a' .. 'z'];
hlp_digit = ['0' .. '9'];
hlp_id_head = (hlp_letter | ('_' (hlp_letter | hlp_digit)));
hlp_id_tail = (('_')? (hlp_letter | hlp_digit))*;
hlp_identifier = hlp_id_head hlp_id_tail;
/*----------------------------------------------------*/
Tokens
/*----------------------------------------------------*/
tk_program = hlp_program;
tk_endprogram = hlp_endprogram;
tk_ignore = hlp_tab | hlp_eol | hlp_space;
tk_identifier = hlp_identifier;
/*-------------------------------------------------*/
Ignored Tokens
/*-------------------------------------------------*/
tk_ignore;
/*---------------------------------------------------*/
Productions
/*----------------------------------------------------*/
aspect {-> aspect} =
lib_elem* {-> New aspect([lib_elem.lib_elem])}
;
lib_elem {-> lib_elem} = tk_program [name]:tk_identifier tk_endprogram {-> New lib_elem.program(name, Null)}
;
/*-----------------------------------------*/
Abstract Syntax Tree
/*-----------------------------------------*/
aspect = [lib_elems]:lib_elem*
;
lib_elem = {program} [name]:tk_identifier [id]:identifier
;