6 ms·
How do I build a grammar tree is akin to saying how is a language read. Grammar tree, like language depends on what grammar is read. e.g. japanese grammar handl
by sargstuff 5y ago
How do I build a grammar tree is akin to saying how is a language read. Grammar tree, like language depends on what grammar is read. e.g. japanese grammar handled bit differently than english grammar
The wierd thing about [1] is the grammer symbols section rules do not define 100% of valid lexical tokens. Where tokens are implied in grammer symbols section, see relevant rules section. If have tokenized things, then just need to used/read in token(s), search grammar rule tree till find terminal token that matches lvalue, then return to priror level & complete rest of grammar rule, etc.. until get back up to top of tree.
shell example:
variable = "asdf"
Do an in order traversl to parse grammar. (recursive desent will make remembering where at & moving back up grammar tree bit easier -- function call will retain how much of grammar rule have done at a given tree level)
To set a variable, recursively walk through grammar lvalue rules array/tree and evaluating the rvalues in the lvalue rules until current token matches an lvalue rule.
aka grammar rules 1st lvalue in tree is program.
Example walkthrough of shell program variable = "asdf"
1) tokenizing variable = "asdf" gives
1st l_value token is WORD_ASSIGNMENT
WORD_ASSIGNMENT has the value -> variable
2rd l_value token is WORD
WORD has a value -> "asdf"
3nd l_value token is NEWLINE
2) grammar tree walk notation:
(lvalue) means, doing next set of rvalues (bump in indentation)
if (lvalue) isn't before "->" means rvalue of an lvalue rule in 1st line above it that include (lvalue).
Grammar tree rules walkthrough trace. (top of tree is PROGRAM rule):
program (lvalue)->
linebreak(lvalue) -> /*empty*/
complete_commands (lvalue)->
complete_commands -> nomatch
newline_list -> no match
complete_command(lvalue) ->
list(lvalue) ->
list-> no match
seperator_op -> no_match
and_or -> no match
linebreak -> /*empty*/
Note: have reached the end of the lvalue "program",
so have all the 'rvalues" needed to generate
the program lvalue.
apply section 2.10.1, 'WORD_ASSIGNMENT'
(tokenizer should have recognized WORD before =), generate a storage location for the variable name contained in WORD_ASSIGNMENT token.
if next fetched token is WORD, then assign store value of WORD token to WORD_ASSIGNMENT variable.