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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -