Subtopic 5 of 6
2.5Maximal munch and rule priority
The lexer takes the longest prefix matching any pattern; if two rules match the same length, the earlier rule wins (so keywords listed before identifiers).
Rules in priority order: KEYWORD = if|else|while|int · ID = [A-Za-z_]\w* · NUM = \d+(\.\d+)? · RELOP = <=|>=|==|<|> · ASSIGN = = · OP = [-+*/] · PUNCT = [(){};,]
- at "ifx <= i": KEYWORD→"if", ID→"ifx". Longest wins: <ID, ifx>
- at "<= iffy ": RELOP→"<=". Longest wins: <RELOP, <=>
- at "iffy == ": KEYWORD→"if", ID→"iffy". Longest wins: <ID, iffy>
- at "== 3": RELOP→"==", ASSIGN→"=". Longest wins: <RELOP, ==>
- at "3": NUM→"3". Longest wins: <NUM, 3>
Previous year questions: Lexical Analysis (2)
PYQs are grouped by topic, so this is the full set for Lexical Analysis; try the ones that match this subtopic. Open all 2 on the topic page.