Add theory application pages (conflicts, semantic actions, full example)#263
Add theory application pages (conflicts, semantic actions, full example)#263halotukozak wants to merge 6 commits intogrammar-theoryfrom
Conversation
- Formal definition block for Parse Table Conflict (state/symbol pair collision) - Shift/reduce conflict section with CalcParser 1+2+3 example and real Alpaca error message - alwaysBefore/alwaysAfter discrepancy note immediately after error block - Reduce/reduce conflict section with Integer/Float example and error message - LR(1) lookahead disambiguation section - Resolution by priority section with minimal sc:nocompile example (production.plus only) - Compile-time detection section with standard blockquote callout - Cross-links to cfg.md, shift-reduce.md, ../conflict-resolution.md, semantic-actions.md, full-example.md
- Six-step narrative from bare grammar to working calculator (7.0) - CalcLexer definition, bare CalcParser with ShiftReduceConflict error - Resolved CalcParser with all 6 resolutions using production.div (not production.divide) - Pipeline evaluation: 1+2*3=7.0, (1+2)*3=9.0 with null-check note - Semantic action trace for 1+2*3 showing 2*3 reduces before 1+... - Formal definition block, compile-time callout blockquote - Theory-to-code mapping table with cross-links to all theory pages
- Formal definition block for Semantic Action (S-attributed scheme) - Syntax-directed translation section with synthesized attribute explanation - Extractor pattern section with complete 7-production CalcParser action table - No-parse-tree section grounded in Parser.scala loop() implementation - Typed results section explaining Rule[Double] compile-time type checking - Compile-time processing callout - Cross-links to shift-reduce.md, conflicts.md, ../extractors.md, ../parser.md, full-example.md - No Rule[Int], no n.value.toDouble, no inherited attribute, no L-attributed
There was a problem hiding this comment.
Pull request overview
This PR adds three comprehensive theory documentation pages that bridge compiler theory concepts with Alpaca's practical implementation. The pages complete Phase 10 of the v1.1 Compiler Theory Tutorial milestone by demonstrating how conflicts, semantic actions, and the full calculator example work together.
Changes:
- Adds formal treatment of parse table conflicts (shift/reduce and reduce/reduce) with error message examples and resolution strategies
- Adds semantic actions documentation explaining S-attributed translation, extractor patterns, and typed results without exposing parse trees
- Adds complete end-to-end calculator example showing the progression from bare grammar to resolved parser with execution trace
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| docs/_docs/theory/conflicts.md | Defines parse table conflicts formally, explains shift/reduce and reduce/reduce conflicts, documents the before/after resolution DSL, and notes the alwaysBefore/alwaysAfter error message discrepancy |
| docs/_docs/theory/semantic-actions.md | Explains S-attributed translation scheme, maps formal semantic action notation to Alpaca's case syntax, provides complete production action table, explains immediate evaluation without parse tree construction |
| docs/_docs/theory/full-example.md | Walks through 6-step calculator implementation from lexer to running code with 7.0 result, includes semantic action trace, theory-to-code mapping table, and compile-time processing explanation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Reduce Integer -> Number vs Reduce Float -> Number | ||
| In situation like: | ||
| Number ... |
There was a problem hiding this comment.
The terminal symbol should be "NUMBER" (all caps) not "Number" to match the terminal naming convention consistently used throughout the documentation. The example productions should be "Integer → NUMBER" and "Float → NUMBER", and the error message should display "Reduce Integer -> NUMBER vs Reduce Float -> NUMBER".
| Reduce Integer -> Number vs Reduce Float -> Number | |
| In situation like: | |
| Number ... | |
| Reduce Integer -> NUMBER vs Reduce Float -> NUMBER | |
| In situation like: | |
| NUMBER ... |
|
|
||
| This is why `CalcParser.parse(lexemes)` returns a named tuple `(ctx: Ctx, result: Double | Null)` — not a tree. The semantic actions produce the final value during the parse itself. | ||
|
|
||
| Decision confirmed: "Parse tree never exposed in Alpaca — semantic actions evaluated immediately during LR(1) reduce; parse() returns typed value directly." (STATE.md) |
There was a problem hiding this comment.
This line references "STATE.md", which appears to be an internal project management file that is not part of the user-facing documentation. This reference should be removed as users won't have access to this file and it disrupts the flow of the documentation.
| Decision confirmed: "Parse tree never exposed in Alpaca — semantic actions evaluated immediately during LR(1) reduce; parse() returns typed value directly." (STATE.md) | |
| This design is intentional: the parse tree is never exposed in Alpaca — semantic actions are evaluated immediately during LR(1) reduce, and `parse()` returns the typed value directly. |
- Add 'Compiler Theory' subsection with 9 theory pages in pipeline order - Pages use theory/pagename.md format resolving to docs/_docs/theory/ - Order: pipeline, tokens, lexer-fa, cfg, why-lr, shift-reduce, conflicts, semantic-actions, full-example
- pipeline.md: tokens.md and lexer-fa.md sibling links no longer use theory/ prefix - pipeline.md: lexer.md and parser.md reference doc links now use ../ prefix - tokens.md: cfg.md sibling link no longer uses theory/ prefix - lexer-fa.md: cfg.md sibling link no longer uses theory/ prefix
Integrate theory section: sidebar, compile verify, cross-link fixes
Summary
(s, t, Shift/Reduce), shift/reduce conflict with1+2+3example and real Alpaca error message, alwaysBefore/alwaysAfter discrepancy callout, reduce/reduce conflict,before/afterresolution DSL bridgef(v₁,...,vₙ) → vₐ, maps{ $$ = $1 + $3 }notation to Alpaca'scase (Expr(a), PLUS(_), Expr(b)) => a + b, full 7-production CalcParser table, no-parse-tree explanation (actions fire immediately on reduce)ShiftReduceConflictcompile error → resolved CalcParser →1+2*3=7.0, semantic action trace, theory-to-code mapping table cross-linking all 8 prior theory pagesPart of the v1.1 Compiler Theory Tutorial milestone — Phase 10: Application (TH-07, TH-08, TH-09).
Test plan
./mill docJarpasses (allsc:nocompileblocks correctly annotated)theory/conflicts.mdhas formal conflict definition and alwaysBefore/alwaysAfter notetheory/semantic-actions.mdsays "S-attributed", no "inherited attribute" /Rule[Int]theory/full-example.mdusesproduction.div(notproduction.divide),Rule[Double], shows7.0result> **Compile-time processing:**callout and cross-links to reference docs🤖 Generated with Claude Code