EBNF parser in php -
i want implement ebnf parser in php without using inbuilt php functions such preg_match. want make 4 functions expr(),term(),factor() , digit() , function match tokens. don't understand how write it. tried way still not working.
ebnf is
exp ::= term {(+ | -) term} term ::= factor {(* | /) factor} factor ::= (exp) | digit digit ::= 0 | 1 | 2 | 3
can please me write it? appreciate .
thank much.
edited:
here recursive decent recognizer(pseudo code) implement in php.
procedure start
exp( ) if token < >$ /* blank ended $ error */ error;
procedure exp
term( )
while (token==’+’ || token==’-’)
{
if token = = ‘+’ match (‘+’) term ( ) else if token = = ‘-’ match (‘-’) term ( ) else if token = = ‘$’ success...!!! else error( );
}
procedure term
factor( )
while (token==’*’ || token==’/’)
{
if token = = ‘*’ match (‘*’) factor ( ) else if token = = ‘/’ match (‘/’) factor ( ) else error( );
}
procedure factor
digit ( )
while (token==’(’ )
{ if token = = ‘(’
match(‘(’) exp( ); if token=(‘)’) match(‘)’) ; else error( );
}
procedure digit
if token in {0,1,2,3} match (token) else error ( )
procedure match (t)
if token = = t token = next token -- - - - ------------- advanced pointer else error( )
procedure error
print (“error......”);
Comments
Post a Comment