Lets assume a statement m=a+b*c-4 written in some programming language (e.g. C ). So for running this statement we have to translate the statement into binary code or machine language which will then gets executed. The gcc or Turbo C compiler translate the above statement with the help of following six phases.
- Lexical Analysis: Every high level programming language is made up of some constructs like operators, keywords, expression, methods, syntax and the rules to write any statement. To translate the statement first the machine must understands the meaning of all the components of the statement m=a+b*c-4. These components are called as tokens means the meaningful constructs of any programming language. m, a, b, c are identifiers; =, +, - are operators. So these constructs are called as tokens. Identifiers, keywords, functions, delimiters, numbers etc are the tokens. Lexical analysis convert the statement into statement of tokens with lexeme. Lexeme means the pattern for a particular type of token. e.g. = is a lexeme of type operator token and m is a lexeme of type identifier token with a pattern represented by Regular Expression. The regular expression for digit (here 4) is [0-9]+. So the lexical expression is like id=id+id*id-digit. The identifiers are stored in symbol table to record the attributes of the identifier like its data type, value etc which will be used in further phases.