php - preg_match only some numbers -


i want write preg_match function bnf grammar.

exp ::= exp + term | exp - term | term

term ::= term * factor | term / factor | factor

factor ::= ( exp ) | digit

digit ::= 0 | 1 | 2 | 3

i tried write way

$pattern = "|[0-3+-()*/]+\$$|";

but problem , accepts string 33$ not valid per grammar per grammar 1+22$ should not valid because number should 0|1|2|3.

what mistake making? can please me.

edit:

example: 1+22$ should invalid string because want numbers 0,1,2,3 not number > 3

thank you.

try pattern?

/^([0-3][+-*/])+[0-3]\$$/ 

solution parenths not trivial regexp;

/^(\(?[0-3]\)?[+-*/])+[0-3]\)\$$/ 

allow create right expressions (2+3)$ wrong (2+3$

write own lexer this


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -