Compilers
Which kind of grammar is used to describe most programming languages and underlies parsing?
- ARegular grammars only
- BNatural-language grammar
- CContext-free grammars
Show answer and explanation
Answer: C. Context-free grammars
Nested parentheses and if statements inside if statements cannot be expressed with regular expressions. Most programming-language grammars are written as context-free grammars, collections of rewriting rules such as "expr → expr + term", and parsing is based on them. Parsing methods include LL, which applies rules top-down, and LR, which builds bottom-up, and tools can generate parsers from grammars automatically. Regular grammars suit lexical analysis and cannot handle nesting.
- A. Regular grammars cannot express structures such as nested parentheses.
- B. Programming languages are not defined by natural-language grammar.
- C. Correct. Parsing is based on context-free grammars.