CHAPTER 2 Flashcards

1
Q

What is a formal language?

A

is a set of strings constructed from an alphabet following specific rules (a grammar).

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

What is a regular expression?

A

is a pattern describing a set of strings using characters and operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What is meant by an ambiguous lexical definition?
A

When a single input can be tokenized in multiple valid ways.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Typical examples of ambiguities and how to resolve them:
A

Keyword vs. Identifier: “if” could be a keyword or part of a variable name “ifx”.

Solution: Use longest match (prefer “if” over “ifx”).

Floating-point vs. Integer: 3.14 could be INT (3) followed by . and 14, or FLOAT (3.14).

Solution: Define FLOAT → [0-9]+.[0-9]+ with higher priority than INT.

Operators vs. Multi-character symbols:&raquo_space;= vs.&raquo_space; and =.

Solution: Define&raquo_space;= before&raquo_space; in lexical rules.

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

What is a lexical action?

A

An action executed when a token is matched by the lexer.

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

Example of constructing an NFA for a lexical definition:

A

Regex: (a|b)*ab

NFA Construction:

Start with an ε-transition for a|b.
Use multiple paths for a and b.
Ensure at least one ab is at the end.
scss
Copy
Edit
(ε) → (a) → (b) → (final)
(ε) → (b) → (a) → (final)

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

How is rule priority handled in the implementation?

A

Longest match: The lexer prefers the longest possible token match.

EOF (End of File): The lexer returns an EOF token when the input is exhausted.

Whitespace: Often skipped unless meaningful (e.g., indentation in Python).

Errors: Unrecognized characters trigger a lexical error.

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

What are lexical states? When are they useful?

A

Lexical states allow the lexer to behave differently depending on the current context.

Useful for:

Handling multi-line comments (/* … /) – Switch to a comment state inside / and return to normal when */ is found.
Processing string literals – Inside “…”, different rules apply.
Distinguishing language modes – Example: inside a template string in JavaScript.

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