This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
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
5 changed files
with
119 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,11 @@ | ||
expr_bison: | ||
flex lex.l && bison -t -d syntax.y | ||
gcc syntax.tab.c -lfl -ly -shared -fPIC -o libexpr.so | ||
@echo "\e[92mTesting BISON implementation...\e[0m" | ||
@python3 expr_test.py ||: | ||
expr_stack: | ||
gcc stack.c -shared -fPIC -o libexpr.so | ||
@echo "\e[92mTesting STACK implementation...\e[0m" | ||
@python3 expr_test.py ||: | ||
clean: | ||
@rm -f lex.yy.c syntax.tab.* *.out *.so |
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,25 @@ | ||
#!/usr/bin/env python3 | ||
# encoding: utf-8 | ||
|
||
import ctypes | ||
import os | ||
|
||
|
||
cwd = os.getcwd() | ||
lib_path = os.path.join(cwd, 'libexpr.so') | ||
lib = ctypes.cdll.LoadLibrary(lib_path) | ||
|
||
def convert(expr): | ||
func = lib.convert | ||
func.restype = ctypes.c_char_p | ||
expr_b = expr.encode() | ||
expr_buf = ctypes.c_char_p(expr_b) | ||
return func(expr_buf).decode() | ||
|
||
|
||
assert convert('5')=='5', convert('5') | ||
|
||
assert convert('9-5+2')=='95-2+', convert('9-5+2') | ||
|
||
print("Testing completed.") | ||
|
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,12 @@ | ||
%{ | ||
#include"syntax.tab.h" | ||
%} | ||
%% | ||
[0-9] { | ||
yylval = (char*)malloc(2); | ||
memcpy(yylval, yytext, 2); | ||
return TERM; | ||
} | ||
"+" { yylval = "+"; return ADD; } | ||
"-" { yylval = "-"; return SUB; } | ||
. { /* do nothing */ } |
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,39 @@ | ||
#include<stdlib.h> | ||
#include<stdio.h> | ||
#include<string.h> | ||
#define STACK_LEN 32 | ||
|
||
char *convert(char *expr){ | ||
char opstack[STACK_LEN]; | ||
char stack[STACK_LEN]; | ||
int optop, top; | ||
char *c, *ret; | ||
|
||
memset(opstack, '\0', STACK_LEN); | ||
memset(stack, '\0', STACK_LEN); | ||
optop = top = 0; | ||
c = expr; | ||
|
||
while(*c != '\0'){ | ||
if((*c >= '0') && (*c <= '9')){ | ||
|
||
} | ||
else if((*c == '+') || (*c == '-')){ | ||
|
||
} | ||
else{ | ||
// lexical errors | ||
} | ||
c++; | ||
} | ||
|
||
ret = (char*)malloc(strlen(stack)+1); | ||
sprintf(ret, "%s", stack); | ||
return ret; | ||
} | ||
|
||
int main(){ | ||
puts( convert("3-7+4-5") ); | ||
return 0; | ||
} | ||
|
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,32 @@ | ||
%{ | ||
#include"lex.yy.c" | ||
void yyerror(char*s){} | ||
|
||
/* | ||
* We make assumptions on the input: | ||
* there are no syntax errors, and | ||
* only consist two operators, plus | ||
* and minus, and all numbers contain | ||
* exactly one digit, no spaces. | ||
*/ | ||
|
||
// where reverse Polish notation goes | ||
char *result; | ||
%} | ||
%define api.value.type { char* } | ||
%token TERM ADD SUB | ||
|
||
%% | ||
Eval: Expr { result = $1; } | ||
Expr: TERM /* write the */ | ||
| Expr ADD TERM /* SDT actions */ | ||
| Expr SUB TERM /* at HERE */ | ||
%% | ||
|
||
char *convert(char *expr){ | ||
YY_BUFFER_STATE buf; | ||
buf = yy_scan_string(expr); | ||
yyparse(); | ||
yy_delete_buffer(buf); | ||
return result; | ||
} |