Skip to content

Add theory application pages (conflicts, semantic actions, full example)#263

Open
halotukozak wants to merge 6 commits intogrammar-theoryfrom
theory-conflicts-page
Open

Add theory application pages (conflicts, semantic actions, full example)#263
halotukozak wants to merge 6 commits intogrammar-theoryfrom
theory-conflicts-page

Conversation

@halotukozak
Copy link
Copy Markdown
Owner

Summary

  • theory/conflicts.md — Conflicts & Disambiguation: formal parse table conflict definition (s, t, Shift/Reduce), shift/reduce conflict with 1+2+3 example and real Alpaca error message, alwaysBefore/alwaysAfter discrepancy callout, reduce/reduce conflict, before/after resolution DSL bridge
  • theory/semantic-actions.md — Semantic Actions & Typed Results: S-attributed translation scheme, formal definition f(v₁,...,vₙ) → vₐ, maps { $$ = $1 + $3 } notation to Alpaca's case (Expr(a), PLUS(_), Expr(b)) => a + b, full 7-production CalcParser table, no-parse-tree explanation (actions fire immediately on reduce)
  • theory/full-example.md — Full Calculator: Theory to Code (185 lines): 6-step narrative from bare grammar → ShiftReduceConflict compile error → resolved CalcParser → 1+2*3=7.0, semantic action trace, theory-to-code mapping table cross-linking all 8 prior theory pages

Part of the v1.1 Compiler Theory Tutorial milestone — Phase 10: Application (TH-07, TH-08, TH-09).

Test plan

  • ./mill docJar passes (all sc:nocompile blocks correctly annotated)
  • theory/conflicts.md has formal conflict definition and alwaysBefore/alwaysAfter note
  • theory/semantic-actions.md says "S-attributed", no "inherited attribute" / Rule[Int]
  • theory/full-example.md uses production.div (not production.divide), Rule[Double], shows 7.0 result
  • All pages have > **Compile-time processing:** callout and cross-links to reference docs

🤖 Generated with Claude Code

- 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
Copilot AI review requested due to automatic review settings February 21, 2026 08:34
@github-actions github-actions bot added documentation Improvements or additions to documentation thesis labels Feb 21, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +42 to +44
Reduce Integer -> Number vs Reduce Float -> Number
In situation like:
Number ...
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Suggested change
Reduce Integer -> Number vs Reduce Float -> Number
In situation like:
Number ...
Reduce Integer -> NUMBER vs Reduce Float -> NUMBER
In situation like:
NUMBER ...

Copilot uses AI. Check for mistakes.

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)
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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.

Copilot uses AI. Check for mistakes.
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation thesis

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants