-
Notifications
You must be signed in to change notification settings - Fork 0
example05
Based on the 5th shift/reduce conflict, we create the following options for the user:
(* Example 05 *)
(Option 1) | (Option 2)
|
PLUS | IF
/ \ | / | \
IF expr | cexpr expr PLUS
/ | \ | / \
cexpr expr expr | expr expr
|
(IF cexpr THEN expr ELSE expr) PLUS expr | IF cexpr THEN expr ELSE (expr PLUS expr)Let's suppose that the user selected Option 1.
We follow the Discota algorithm modified for the context of applying tree automata in language design to learn a tree automaton that encodes the above example.
Generate states
-
$Q_5$ +=${ X, Y, \epsilon }$ -
$S_5$ =${ X }$
Step 2: Rewrite the example tree as well as the representative sample w.r.t. the newly generated states
With the newly generated states, the selected example tree can be represented as follows:
PLUS
/ \
IF2 X
/ | \
D Y Y
(IF D THEN Y ELSE Y) PLUS XThe representative sample
Step 3: Add $\varepsilon$ -transitions and $()$ -transitions based on the parent-child relationship, and $\mathbb{B}$ -transition as well as $\mathbb{N}$ -transition to the corresponding bottom-most leaf states
Add
-
$\Delta_5$ +=${ X \rightarrow_{ε} Y ; ; \ ;;;;;;;;;;; Y \rightarrow_{()} X }$
Step 4: As for the operators that do not appear in the representative sample, encode transitions with the top-most root state
Out of all the alphabets excluding
Because we do not want to exclude any operators after taking intersection with the automaton from original language, we want to add transitions for the $$
Step 5: Based on the parent-children relations, learn the transitions. Verify the resulting transitions with the Angluin's tree automata-learning framework
Based on the precedences specified by the selected tree-structured example (re-written with
-
$\Delta_5'$ =${ X \rightarrow_{+} X ; X ; ; \ ;;;;;;;;;; Y \rightarrow_{IF_2} D ; Y ; Y }$ -
$\Delta_5$ +=$\Delta_5'$
Next, we verify that the above
According to the Angluin's algorithm, an observation table
- Rows
$S(\mathcal{E}_5)$ (subterms):${ +(IF_2(D, Y, Y), X), IF_2(D, Y, Y), D, Y, X }$ - Columns
$\mathcal{E}_5[◇]$ (contexts):${ ◇, +(◇, X), +(IF_2(◇, Y, Y), X), +(IF_2(D, ◇, Y), X), +(IF_2(D, Y, ◇), X), +(IF_2(D, Y, Y), ◇) }$
Suppose that a cell indexed by the subterm
Here,
With this, we can construct the following observation table
| 1 | 1 | 0 | 0 | 0 | 1 | |
| 1 | 1 | 0 | 1 | 1 | 1 | |
| 0 | 0 | 1 | 0 | 0 | 0 | |
| 1 | 1 | 0 | 1 | 1 | 1 | |
| 1 | 1 | 0 | 0 | 0 | 1 |
Let's take a look at some of these contents. For the bold 1s, it is obvious that
Consistency of
In order to ensure that this table has reached a fixed point (TODO: rephrase) thus a valid
Step 7: Take intersection of the above tree automaton with the tree automaton of the original language
Rename the states for readability.
cond_expr:
| TRUE { Bool true }
| FALSE { Bool false }
;
expr_1:
| expr_1 PLUS expr_1 { Plus ($1, $3) }
| expr_1 MUL expr_1 { Mul ($1, $3) }
| e = expr_2 { e } (* epsilon-transition *)
| IF cond_expr THEN expr_1 { If ($2, Then ($4, Else Na)) }
;
expr_2:
| INT { Int $1 }
| LPAREN expr_1 RPAREN { Paren $2 }
| IF cond_expr THEN expr_2 ELSE expr_2 { If ($2, Then ($4, Else $6)) }
;Once the above is encoded, the number of shift/reduce conflicts decreases from 9 to 6. Note that this also resolves the shift/reduce conflict from Example09 (aka, dangling-else ambiguity) along with Example06.