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
4 changes: 2 additions & 2 deletions samples/CalcExpr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This project implements a simple mathematical expression parser.

```sh
start
= sum_expr EOF
= S sum_expr EOF
;

sum_expr
Expand Down Expand Up @@ -37,7 +37,7 @@ S
= [ \t\r\n]*
;

EOF:
EOF
= $
;
```
8 changes: 4 additions & 4 deletions samples/TinyC/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public abstract record Node
public static Node Empty() => new BlockNode([]);
public static Node Number(int value) => new NumberNode(value);
public static Node Variable(string name) => new VariableNode(name);
public static Node If(Node test, Node ifTrue, Node ifFalse) => new IfNode(test, Wrap(ifTrue), Wrap(ifFalse));
public static Node If(Node test, Node ifTrue, Node ifFalse) => new IfNode(test, Block(ifTrue), Block(ifFalse));
public static Node Ternary(Node test, Node ifTrue, Node ifFalse) => new TernaryNode(test, ifTrue, ifFalse);
public static Node WhileLoop(Node test, Node body) => new WhileLoopNode(test, Wrap(body));
public static Node DoWhileLoop(Node test, Node body) => new DoWhileLoopNode(test, Wrap(body));
public static Node WhileLoop(Node test, Node body) => new WhileLoopNode(test, Block(body));
public static Node DoWhileLoop(Node test, Node body) => new DoWhileLoopNode(test, Block(body));
public static Node Unary(char op, Node operand) => new UnaryNode(op, operand);
public static Node Binary(string op, Node left, Node right) => new BinaryNode(op, left, right);
public static Node Assign(Node variable, Node value) => Binary("=", variable, value);
public static Node Block(IReadOnlyList<Node> statements) => new BlockNode(statements);
private static Node Wrap(Node statement) => statement is BlockNode ? statement : new BlockNode([statement]);
public static Node Block(Node statement) => statement is BlockNode ? statement : new BlockNode([statement]);

public sealed override string ToString()
{
Expand Down
32 changes: 17 additions & 15 deletions samples/TinyC/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Tiny-C
# Tiny-C

This project implements a parser for the [Tiny-C](http://www.iro.umontreal.ca/~felipe/IFT2030-Automne2002/Complements/tinyc.c) language, a highly simplified version of `C` designed as an educational tool for learning about compilers.

Expand All @@ -11,7 +11,7 @@ The main differences from the original `Tiny-C` are:
## Tiny-C Grammar

```sh
start:
start
= S statement EOF
;

Expand All @@ -20,10 +20,12 @@ keyword
;

number
= [0-9]+;
= [0-9]+
;

variable
= !keyword [a-zA-Z_][a-zA-Z0-9_]*;
= !keyword [a-zA-Z_][a-zA-Z0-9_]*
;

S
= [ \t\n\r]*
Expand All @@ -43,34 +45,34 @@ number_expr

expr
= assigment_expr
/ ternary_expr
;

assigment_expr
= var_expr "=" S expr
/ ternary_expr
;

ternary_expr
= or_expr ("?" S expr ":" S ternary_expr)?
= logical_or_expr ("?" S expr ":" S ternary_expr)?
;

or_expr
= and_expr ("||" S and_expr)*
logical_or_expr
= logical_and_expr ("||" S logical_and_expr)*
;

and_expr
= inclusive_or_expr ("&&" S inclusive_or_expr)*
logical_and_expr
= bitwise_or_expr ("&&" S bitwise_or_expr)*
;

inclusive_or_expr
= exclusive_or_expr ("|" S exclusive_or_expr)*
bitwise_or_expr
= bitwise_xor_expr ("|" S bitwise_xor_expr)*
;

exclusive_or_expr
= binary_and_expr ("^" S binary_and_expr)*
bitwise_xor_expr
= bitwise_and_expr ("^" S bitwise_and_expr)*
;

binary_and_expr
bitwise_and_expr
= eq_expr ("&" S eq_expr)*
;

Expand Down
Loading