CHAPTER 3 Flashcards

1
Q

Construct a CFG for a simple part of a programming language

A

consists of a set of rules (productions) that describe a language.

Example CFG for arithmetic expressions:

E → E + T | E - T | T
T → T * F | T / F | F
F → ( E ) | id | num

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a nonterminal symbol? A terminal symbol? A production? A start symbol? A parse tree?

A

Nonterminal symbol: A variable that can be replaced by other symbols (e.g., E, T, F in the above CFG).

Terminal symbol: A symbol that appears in the final output (e.g., +, *, num).

Production: A rule of the form A → α, where A is a nonterminal, and α is a sequence of terminals and/or nonterminals.

Start symbol: The initial nonterminal from which derivations begin (e.g., E in our CFG).

Parse tree: A hierarchical tree structure representing how a string is derived from the grammar.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the left-hand side (LHS) of a production? The right-hand side (RHS)?

A

LHS: The nonterminal being defined (E → …, here E is the LHS).

RHS: The sequence that replaces the nonterminal (E → E + T | T, here E + T and T are RHS).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given a grammar G, what is meant by the language L(G)?

A

The set of all valid strings derivable from the start symbol using the production rules.

Example: If G is the arithmetic expression grammar, then L(G) includes “id + num”, “num * id + num”, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a derivation step? A derivation? A leftmost derivation? A rightmost derivation?

A

Derivation step: Applying one production rule (e.g., E → E + T).

Derivation: A sequence of steps that produces a string from the start symbol.

Leftmost derivation: Always expands the leftmost nonterminal first.

Rightmost derivation: Always expands the rightmost nonterminal first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does a derivation correspond to a parse tree?

A

Each derivation step expands a node in the parse tree.

A leftmost derivation corresponds to a leftmost expansion of the tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does it mean for a grammar to be ambiguous? Unambiguous?

A

Ambiguous grammar: A string has more than one valid parse tree.

Unambiguous grammar: Each string has a unique parse tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between an LL and an LR parser?

A

LL (Left-to-right, Leftmost derivation):
Top-down parsing (predictive parsing).
Looks ahead to decide which production to apply.
Example: Recursive Descent Parser.

LR (Left-to-right, Rightmost derivation):
Bottom-up parsing (shift-reduce).
Constructs the parse tree in reverse.
Example: YACC, Bison parsers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Difference between LL(1) and LL(2)? LR(1) and LR(2)?

A

LL(k): Uses k lookahead tokens.

LL(1) uses 1 lookahead (simpler but weaker).
LL(2) uses 2 lookaheads (handles more complex grammars).

LR(k): Similar but for bottom-up parsers.

LR(1) uses 1 lookahead, LR(2) uses 2 lookaheads for shift-reduce decisions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why are context-free grammars more powerful than regular expressions?

A

Regular expressions only define regular languages (no nested structures).

Context-free grammars can describe recursive structures (like nested parentheses).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In what sense are context-free grammars “context-free”?

A

The rules apply independently of context (i.e., no dependency on neighboring symbols).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly