A few days ago wrote a small language lexical analysis program, because a few days ago in the VC knowledge Base to see a Pascal lexical analysis of the procedure, feel very complex to write. In fact, the principle of lexical analysis procedures are the same, so I think as long as you understand the simple lexical analysis procedures, and then write complex is not difficult, nothing more than a few more keywords, write a few conditions to judge the sentence. Lexical analysis is the basis of the compiler program, but also the simplest. OK, now let's see the procedure.
Let's take a look at the grammar of the small language first.
g[< Program]:
< procedure >∷=< procedure First >;< sub-procedure.
< program header >∷=program< identifier >
< procedures >∷=< Compound statements >
< compound statement >∷=begin< statement sequence >end
< statement sequence >∷=< statement >{;< statement
< statement >∷=< Assignment statement >|< Compound statement >|< conditional Statement >
< Assignment statement >∷=< identifier >:=< expression >
< conditional statement >∷=if < Boolean expression > then < statement > Else < statement >
< expression >∷=< Item >{(+|-) < key
< >∷=< >{(*|/) <-type
< >∷=< identifier >|< unsigned constant >| ' (' < expression > ') '
< Boolean expression >∷=< expression >< relational operator >< expression >
< relational operator >∷= =|<|<=|>|>=|<>
< identifiers >∷=< Letters >{< letters >|< Numbers}
< unsigned constant >∷=< digital >{< digital >}[.< Digital >{<
< letter >∷=a|b|c|d|e|f|g|......| U|v|w|x|y|z
< digital >∷=0|1|2|3|4|5|6|7|8| 9