Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/preview/slr8/0.0.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 sjfhsjfh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 85 additions & 0 deletions packages/preview/slr8/0.0.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# slr8

Takes a grammar and a sentence and walks through SLR(1) parsing: augmented
grammar, FIRST/FOLLOW, canonical LR(0) items, the DFA, the ACTION/GOTO
table, a shift-reduce trace, the parse tree. All computed in Typst, no
external script generating a table beforehand.

It's SLR(1) — LR(0) item sets, but reduce actions only go in when the
symbol's in FOLLOW(LHS). That's the whole difference from plain LR(0).

See [`example.pdf`](docs/example.pdf) for what it looks like end to end on:

```
C → id ( A )
A → A , E | ε | E
E → E + T | T
T → id | num | C
```
parsing `id ( num + id , id ( num + id ) )`.

## Using it

```typst
#import "@preview/slr8:0.0.1": *

#let my-grammar = (
("C", ("id", "(", "A", ")")),
("A", ("A", ",", "E")),
("A", ("\\epsilon",)),
("A", ("E",)),
("E", ("E", "+", "T")),
("E", ("T",)),
("T", ("id",)),
("T", ("num",)),
("T", ("C",)),
)

#let my-sentence = ("id", "(", "num", "+", "id", ",", "id", "(", "num", "+", "id", ")", ")")

#show-grammar(my-grammar)
#show-parse-table(my-grammar)
#show-parse-trace(my-grammar, my-sentence)
#show-parse-tree(my-grammar, my-sentence)
```

A grammar is just a list of `(LHS, RHS)` pairs, RHS being a tuple of
symbols. First production's LHS = start symbol. Anything that's not a LHS
anywhere is a terminal. `"\\epsilon"` for empty productions. Don't use `"."`
as a symbol — it's the LR item dot internally and things will break in
confusing ways if it collides.

A sentence is just the terminals, no `$` at the end, that gets added for
you.

## What each function gives you

`show-grammar` / `show-aug-grammar` — the production list, plain or with the
`S' → S` row added.

`show-first-follow` — FIRST/FOLLOW per non-terminal.

`show-canonical-items` — every I_n state, with where it came from.

`show-automaton(grammar, width: 100%)` — the LR(0) DFA, rendered via
diagraph/Graphviz.

`show-parse-table` — ACTION/GOTO, conflicts called out in red if there are
any.

`show-parse-trace` / `show-parse-tree` — take `(grammar, sentence)`, give
you the stack-by-stack trace and the resulting tree.

All of them take the grammar *before* augmentation — that part's handled
for you.

## Known gaps

- SLR(1) conflict resolution, not LALR/LR(1) — if your grammar genuinely
needs per-state lookahead to be unambiguous, you'll see conflicts here
even though a stronger parser wouldn't have any.
- FIRST-set computation only skips direct left recursion (`A → A α`).
Indirect left recursion through another non-terminal isn't handled.
- When there's a real shift/reduce or reduce/reduce conflict, whichever
action got inserted first wins — there's no precedence/associativity
table backing this, it's reported and left at that.
Binary file added packages/preview/slr8/0.0.1/docs/example.pdf
Binary file not shown.
103 changes: 103 additions & 0 deletions packages/preview/slr8/0.0.1/docs/example.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#import "@preview/slr8:0.0.1": *

// Use "\\epsilon" for empty productions
// The first production's LHS is treated as the start symbol
// The dot character "." is reserved, do not use it as a symbol

#let my-grammar = (("C", ("id", "(", "A", ")"))
,("A", ("A", ",", "E"))
,("A", ("\\epsilon",))
,("A", ("E",))
,("E", ("E", "+", "T"))
,("E", ("T",))
,("T", ("id",))
,("T", ("num",))
,("T", ("C",)))

#let my-sentence = ("id", "(", "num", "+", "id", "," , "id", "(" , "num", "+" , "id" , ")" , ")")

// DOCUMENT (comment out any section you don't need)

#set page(margin: 2cm)
#set text(font: "New Computer Modern", size: 10pt)

#align(center)[
#text()[Your Little Name]

#text(size: 16pt, weight: "bold")[SLR Parser Visualization]

#v(4pt)

#text(weight: "bold",size: 10pt)[
Input: #raw(my-sentence.join(" "))
]
]

#v(16pt)


#grid(
columns: (1fr, 1fr),
align: center,
[
== Original grammar

#v(5pt)

#show-grammar(my-grammar)
],
[
== Augmented grammar

#v(5pt)

#show-aug-grammar(my-grammar)
]
)


== First and Follow sets

#v(5pt)

#show-first-follow(my-grammar)

#v(16pt)

== Set of items

#v(5pt)

#show-canonical-items(my-grammar)

#v(16pt)

== DFA

#v(12pt)

#show-automaton(my-grammar)

#v(18pt)

== States table

#v(16pt)

#align(center, show-parse-table(my-grammar))

#pagebreak()

== Stack for #raw(my-sentence.join(" "))

#v(16pt)

#show-parse-trace(my-grammar, my-sentence)

#pagebreak()

== Parse Tree for #raw(my-sentence.join(" "))

#v(16pt)

#show-parse-tree(my-grammar, my-sentence)
Loading
Loading