add rule and find some issue

This commit is contained in:
Tan, Kian-ting 2023-09-22 01:45:47 +08:00
parent 5f0943539e
commit 1a7598fd6e
5 changed files with 49 additions and 20 deletions

View file

@ -12,3 +12,4 @@ License: MIT
- 20230910 : add basic parser `CONST` rule, and add the grammar rule. - 20230910 : add basic parser `CONST` rule, and add the grammar rule.
- 20230914-15: 追加一寡 tokenizer ê 功能。 - 20230914-15: 追加一寡 tokenizer ê 功能。
- 20230918: 重新tuì下kàu頂起做parser. add rule - 20230918: 重新tuì下kàu頂起做parser. add rule
- 20230921-22:add rule, report issue

BIN
lexicalSyntaxesDraft.pdf Normal file

Binary file not shown.

View file

@ -1 +1,5 @@
expr = int | int add int # expr1 and #expr2 single = "(" expr ")" | int
fac1 = applier "(" int ")" | single
applier = expr
term = fac | fac (MUL | DIV) fac
expr = term (ADD | SUB) term

View file

@ -101,6 +101,7 @@ let tMul = m1TType(tk.TokenType.I_MUL);
let tDiv = m1TType(tk.TokenType.I_DIV); let tDiv = m1TType(tk.TokenType.I_DIV);
let tLParen = m1TType(tk.TokenType.L_PAREN); let tLParen = m1TType(tk.TokenType.L_PAREN);
let tRParen = m1TType(tk.TokenType.R_PAREN); let tRParen = m1TType(tk.TokenType.R_PAREN);
let toSome = tk.toSome;
node_process_1.argv.forEach((val, index) => { node_process_1.argv.forEach((val, index) => {
console.log(`${index}=${val}`); console.log(`${index}=${val}`);
}); });
@ -160,17 +161,27 @@ let circumfix = (f, signal) => (x) => {
var a = f(x); var a = f(x);
if (a._tag == "Some") { if (a._tag == "Some") {
let inner = a.value.ast[a.value.ast.length - 2]; let inner = a.value.ast[a.value.ast.length - 2];
console.log("AST====" + repr(a.value.ast));
let ast_middle = [inner]; let ast_middle = [inner];
let new_ast = [ast_middle]; let new_ast = [ast_middle];
a.value.ast = new_ast; a.value.ast = new_ast;
console.log("+" + signal + "+" + repr(a));
} }
return a; return a;
}; };
/** fac1 = "(" expr ")" */ /**
let fac1 = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1"); * TODO: 12(13)(14) only parsed with only 12(13)
let fac2 = tInt; */
/** single1 = tInt | "(" expr ")"*/
let single1 = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1");
let single2 = tInt;
let single = orDo(single1, single2);
/** fac1 = single "(" int ")" | single */
let fac1Appliee = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), tInt), tRParen), "fac1");
let fac1 = (x) => {
let n = thenDo(thenDo(toSome(x), single), fac1Appliee);
console.log("+" + "bocchitherock" + "+" + repr(n));
return n;
};
let fac2 = single;
let fac = orDo(fac1, fac2); let fac = orDo(fac1, fac2);
/** /**
* *
@ -199,7 +210,9 @@ let expr2 = term;
* expr = expr1 | expr2 * expr = expr1 | expr2
*/ */
let expr = orDo(expr1, expr2); let expr = orDo(expr1, expr2);
let tokens = tk.tokenize("(4-(3/4))"); //tk.tokenize(argv[2]); let tokens = tk.tokenize("12(13)(14)");
//let tokens = tk.tokenize("(4-(3/4))");
//tk.tokenize(argv[2]);
let tokensFiltered = tokens.filter((x) => { let tokensFiltered = tokens.filter((x) => {
return (x.type != tk.TokenType.NL return (x.type != tk.TokenType.NL
&& x.type != tk.TokenType.SP); && x.type != tk.TokenType.SP);
@ -214,4 +227,4 @@ let beta = expr({
remained: tokensFiltered, remained: tokensFiltered,
ast: [] ast: []
}); });
console.log(repr(beta)); console.log("RESULT=" + repr(beta));

View file

@ -101,6 +101,7 @@ let tDiv = m1TType(tk.TokenType.I_DIV);
let tLParen = m1TType(tk.TokenType.L_PAREN); let tLParen = m1TType(tk.TokenType.L_PAREN);
let tRParen = m1TType(tk.TokenType.R_PAREN); let tRParen = m1TType(tk.TokenType.R_PAREN);
let toSome = tk.toSome;
argv.forEach((val, index) => { argv.forEach((val, index) => {
@ -170,28 +171,36 @@ let circumfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{
var a = f(x); var a = f(x);
if (a._tag == "Some"){ if (a._tag == "Some"){
let inner = a.value.ast[a.value.ast.length-2]; let inner = a.value.ast[a.value.ast.length-2];
console.log("AST===="+repr(a.value.ast));
let ast_middle : tkTree[] = [inner]; let ast_middle : tkTree[] = [inner];
let new_ast = [ast_middle]; let new_ast = [ast_middle];
a.value.ast = new_ast; a.value.ast = new_ast;
console.log("+"+signal+"+"+repr(a));
} }
return a; return a;
} }
/** fac1 = "(" expr ")" */ /**
let fac1 = circumfix((x : TokenMatcheePair)=> * TODO: 12(13)(14) only parsed with only 12(13)
*/
/** single1 = tInt | "(" expr ")"*/
let single1 = circumfix((x : TokenMatcheePair) =>
thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1"); thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1");
let single2= tInt;
let single = orDo(single1, single2);
let fac2 = tInt; /** fac1 = single "(" int ")" | single */
let fac1Appliee = circumfix((x : TokenMatcheePair) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), tInt), tRParen), "fac1");
let fac1 = (x : TokenMatcheePair) =>
{
let n = thenDo(thenDo(toSome(x), single), fac1Appliee);
console.log("+"+"bocchitherock"+"+"+repr(n));
return n
};
let fac2 = single;
let fac = orDo(fac1, fac2); let fac = orDo(fac1, fac2);
/** /**
* *
* term1 = fac (MUL | DIV) fac * term1 = fac (MUL | DIV) fac
@ -231,8 +240,10 @@ let expr = orDo(expr1, expr2);
let tokens = tk.tokenize("12(13)(14)");
//let tokens = tk.tokenize("(4-(3/4))");
//tk.tokenize(argv[2]);
let tokens = tk.tokenize("(4-(3/4))");//tk.tokenize(argv[2]);
let tokensFiltered = tokens.filter( let tokensFiltered = tokens.filter(
(x)=>{return (x.type != tk.TokenType.NL (x)=>{return (x.type != tk.TokenType.NL
&& x.type != tk.TokenType.SP)}); && x.type != tk.TokenType.SP)});
@ -249,5 +260,5 @@ let beta = expr({
ast : []}); ast : []});
console.log(repr(beta)); console.log("RESULT="+repr(beta));