1

I am trying to create a lexical analyzer for a given language. Can't write grammar rules correctly. My task is

The input language contains conditional statements if ... then ... else and if ... then, separated by a symbol ; (semicolon). Condition statements contain identifiers, comparison signs <,>, =, hexadecimal numbers, sign assignments (:=). Consider the sequence as hexadecimal numbers digits and symbols a, b, c, d, e, f starting with a digit (for example, 89, 45ac, 0abc )

I got these rules:

S -> if E then S else S; S | if E then S; S | if E then S else S | if E then S | I := E 
E -> H | E > H | E < H | E = H
I -> LLL
H -> DHL | DL | DH |D
L -> a | b | c | d | e | f
D -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Is the rule (H -> DHL | DL | D | DH) for determining hexadecimal numbers set correctly? Are the rest of the rules correct?

  • 1
    A simpler way of writing the hexadecimal number rule is `H -> D | HD | HL`. But lexical analysers in practice usually use regular expressions, which would be something like `[0-9][0-9a-f]*`. The lexical analyser does not analyse the syntactic structure (in the most usual parser architecture); that's done with a syntactic parser, which does use BNF. – rici Nov 20 '21 at 03:16
  • @rici, yes,I heard that in some compilers, lexical analysis occurs in the syntactic. But my task is process a stream of tokens in a syntax analyzer, where tokens are generated in a lexical analyzer. Thanks for you answer – Миша Попов Nov 20 '21 at 06:34

0 Answers0