M2 – 4/4: Context Free Grammars

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/5

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:23 AM on 4/3/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

6 Terms

1
New cards

What are the nonterminal symbols in a grammar?

A name on the left of a rewrite rule

<p>A name on the left of a rewrite rule </p>
2
New cards

What are the terminal symbols in a grammar?
Example: Non-terminals expr, term, digit-seq

The actual characters that end up in your program, can’t be rewritten further, and terminate the rewriting process: +, -, *, 0-9, (, )

3
New cards
<p>Draw the derivation tree <strong>4+6×2</strong></p>

Draw the derivation tree 4+6×2

Why is 6 a term and 2 a factor?

Same logic as #1, just one level down. The rule used for 6*2 was:

term → term mult-op factor

This has three slots again. * fills mult-op, so:

  • Everything left of * fills the term slot → 6

  • Everything right of * fills the factor slot → 2

<p>Why is <code>6</code> a term and <code>2</code> a factor? </p><p class="font-claude-response-body break-words whitespace-normal leading-[1.7]">Same logic as #1, just one level down. The rule used for <code>6*2</code> was:</p><pre><code>term → term mult-op factor</code></pre><p class="font-claude-response-body break-words whitespace-normal leading-[1.7]">This has three slots again. <code>*</code> fills <code>mult-op</code>, so:</p><ul><li><p>Everything left of <code>*</code> fills the <code>term</code> slot → <code>6</code></p></li><li><p>Everything right of <code>*</code> fills the <code>factor</code> slot → <code>2</code></p></li></ul><p></p>
4
New cards
<p>What are the nonterminal &amp; terminal symbols in this CFG?</p>

What are the nonterminal & terminal symbols in this CFG?

nonterminals: expr, term, factor, add-op, mult-op, digit-seq, digit
terminals:

  • + | -

  • * | DIV | REM

  • 0 | 1 | 2… | 9

5
New cards

When is a string invalid according to the grammar?

  • A symbol appears that no rule can produce (e.g. % isn't in any rule)

  • The structure doesn't match any rule (e.g. two operators in a row 4++2)

  • It's incomplete — you can't finish rewriting all non-terminals (e.g. just +)

  • If you get stuck — meaning no rule can produce what you need — the string is invalid.

6
New cards

What is the standard precedence latter and what does low/high precedence mean?

Low precedence = sits at the top of the tree
High precedence = sits deeper in the tree

The standard precedence ladder (you just have to memorize this)

+ and -     → lowest precedence (top of tree)
* and /     → highest precedence

<p>Low precedence = sits at the top of the tree <br>High precedence = sits deeper in the tree</p><p>The standard precedence ladder (you just have to memorize this) </p><pre><code>+ and -     → lowest precedence (top of tree)
* and /     → highest precedence</code></pre><p></p>

Explore top notes