Recursive descent parser: Difference between revisions

Content deleted Content added
Luqui (talk | contribs)
The previous edit was not a minor edit. Added content about the class of grammars recdescent parsers match, their running time, and a perl module link.
m fix code
Line 13:
We define a procedure <tt>parse_''A''()</tt> for every nonterminal ''A'' that parses a string in the language of ''A''. In this procedure it is first determined with the current symbol in the input stream which rule for the nonterminal will be used. Then it simply calls the procedures <tt>read_ch(''a'')</tt> and <tt>parse_''A''()</tt> for every terminal ''a'' and nonterminal ''A'' in the right-hand side for the rule.
 
{{wikicode}}
'''procedure''' parse_E()
'''begin'''
'''if''' ''ch'' = 'N' '''then''' read_ch('N'); read_ch('O'); read_ch('T'); parse_E(); '''end-if''';
'''if''' ''ch'' = '(' '''then''' read_ch('('); parse_E(); parse_F(); read_ch(')'); '''end-if''';
'''if''' ''ch'' = 'T' '''then''' read_ch('T'); read_ch('R'); read_ch('U'); read_ch('E'); '''end-if''';
'''if''' ''ch'' = 'F' '''then''' read_ch('F'); read_ch('A'); read_ch('L'); read_ch('S'); read_ch('E'); '''end-if''';
'''end'''
 
'''procedure''' parse_Fparse_E() {
'''if''' ''ch'' = 'N' '''then'''{ read_ch('N'); read_ch('O'); read_ch('T'); parse_E(); '''end-if''';}
'''begin'''
'''else if''' ''ch'' = 'A(' '''then'''{ read_ch('A('); read_chparse_E('N'); parse_F() read_ch('D)'); parse_E(); '''end-if''';}
'''else if''' ''ch'' = 'OT' '''then'''{ read_ch('OT'); read_ch('R'); parse_Eread_ch('U'); read_ch('E''end-if''';) }
'''else if''' ''ch'' = 'TF' { read_ch('F''then''') read_ch('TA'); read_ch('RL'); read_ch('US'); read_ch('E'); '''end-if''';}
'''end'''
}
 
'''procedure''' parse_Eparse_F() {
'''if''' ''ch'' = '(A' '''then'''{ read_ch('(A'); parse_Eread_ch('N'); parse_F(); read_ch(')D'); '''end-if''';parse_E() }
'''else if''' ''ch'' = 'FO' '''then'''{ read_ch('FO'); read_ch('AR'); read_chparse_E('L'); read_ch('S'); read_ch('E'); '''end-if''';}
}
 
These procedures use a global variable ''ch'' that contains the current first character in the input stream. The procedure <tt>read_ch(''a'')</tt> reports an error if ''ch'' is not equal to ''a'' and otherwise reads the next character from the input stream into ''ch''.