-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
a.out | ||
program | ||
lexer.* | ||
parser.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lexer.* | ||
parser.* | ||
bin | ||
obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lexer.* | ||
parser.* | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |