Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
lierdakil committed Jun 3, 2020
1 parent 589254e commit 7c5d712
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/polish-notation-calc/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a.out
program
lexer.*
parser.*
22 changes: 22 additions & 0 deletions examples/polish-notation-calc/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "parser.h"
#include <cmath>
#include <iostream>
#include <map>

int main(int argc, char *argv[]) {
bool debug = false;
if (argc > 1) {
debug = (std::string(argv[1]) == "-d");
}
std::string line;
while (std::getline(std::cin, line)) {
try {
Lexer lex(line, debug);
Parser parser(&lex, debug);
auto expr = parser.parse();
std::cout << "Result: " << expr << std::endl;
} catch (std::runtime_error &e) {
std::cerr << "Error while parsing:" << e.what() << std::endl;
}
}
}
24 changes: 24 additions & 0 deletions examples/polish-notation-calc/cpp/syntax.xy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
number /[0-9]+/ text
add /\+/
sub /\-/
mul /\*/
div /\//
pow /\^/
/ +/
%%

%top {
#include <cmath>

using ResultType = double;
}

S : E %eof { _1 }
;
E : add E E { _2 + _3 }
| sub E E { _2 - _3 }
| mul E E { _2 * _3 }
| div E E { _2 / _3 }
| pow E E { std::pow(_2, _3) }
| number { std::stod(_1) }
;
5 changes: 5 additions & 0 deletions examples/polish-notation-calc/cpp/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

${alpaca:-alpaca} -l cpp $args syntax.xy &&\
g++ -std=c++17 -O2 main.cpp lexer.cpp parser.cpp -o program &&\
./program "$@" <<< '+ 1 * 2 ^ 3 4'
4 changes: 4 additions & 0 deletions examples/polish-notation-calc/csharp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lexer.*
parser.*
bin
obj
22 changes: 22 additions & 0 deletions examples/polish-notation-calc/csharp/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using lexer;
using parser;

namespace program
{
class Program
{
static void Main(string[] args)
{
var debug = args.Length > 0 && args[0] == "-d";
var line = Console.ReadLine();
while(line != null) {
var lex = new Lexer(line, debug);
var parser = new Parser(lex, debug);
var expr = parser.parse();
Console.WriteLine($"Result: {expr}");
line = Console.ReadLine();
}
}
}
}
7 changes: 7 additions & 0 deletions examples/polish-notation-calc/csharp/program.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
</Project>
18 changes: 18 additions & 0 deletions examples/polish-notation-calc/csharp/syntax.xy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
number /[0-9]+/ double.Parse(text)
add /\+/
sub /\-/
mul /\*/
div /\//
pow /\^/
/ +/
%%

S : E %eof { _1 }
;
E : add E E { _2 + _3 }
| sub E E { _2 - _3 }
| mul E E { _2 * _3 }
| div E E { _2 / _3 }
| pow E E { Math.Pow(_2, _3) }
| number { _1 }
;
4 changes: 4 additions & 0 deletions examples/polish-notation-calc/csharp/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

${alpaca:-alpaca} -l cs $args syntax.xy &&\
dotnet run "$@" <<< '+ 1 * 2 ^ 3 4'
3 changes: 3 additions & 0 deletions examples/polish-notation-calc/py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lexer.*
parser.*
__pycache__
17 changes: 17 additions & 0 deletions examples/polish-notation-calc/py/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from lexer import Lexer
from parser import Parser
from sys import argv

debug = len(argv) > 1 and argv[1] == '-d'
try:
while True:
line = input()
try:
lex = Lexer(line, debug)
parser = Parser(lex, debug)
expr = parser.parse()
print(f"Result: {expr}")
except Exception as e:
print(f"Error while parsing: {e}")
except EOFError:
"No-op"
18 changes: 18 additions & 0 deletions examples/polish-notation-calc/py/syntax.xy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
number /[0-9]+/ float(text)
add /\+/
sub /\-/
mul /\*/
div /\//
pow /\^/
/ +/
%%

S : E %eof { _1 }
;
E : add E E { _2 + _3 }
| sub E E { _2 - _3 }
| mul E E { _2 * _3 }
| div E E { _2 / _3 }
| pow E E { _2 ** _3 }
| number { _1 }
;
4 changes: 4 additions & 0 deletions examples/polish-notation-calc/py/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

${alpaca:-alpaca} -l py $args syntax.xy &&\
python main.py "$@" <<< '+ 1 * 2 ^ 3 4'
13 changes: 13 additions & 0 deletions examples/polish-notation-calc/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

export alpaca=$(cabal exec -- which alpaca)

for d in cpp csharp py; do
pushd $d
echo "Testing $d..."
for parser in recursive ll1 lr0 lr1 slr lalr; do
echo "Checking $parser parser"
args="-p $parser" ./test.sh "$@"
done
popd
done

0 comments on commit 7c5d712

Please sign in to comment.