From: Tan Kian-ting Date: Thu, 21 Sep 2023 17:45:47 +0000 (+0800) Subject: add rule and find some issue X-Git-Url: https://git.kianting.info/?a=commitdiff_plain;ds=sidebyside;h=1a7598fd6e01515661591a41320a63451cb82d5f;p=clo add rule and find some issue --- diff --git a/README.md b/README.md index f5eb80c..22c2f83 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ License: MIT - 20230910 : add basic parser `CONST` rule, and add the grammar rule. - 20230914-15: 追加一寡 tokenizer ê 功能。 - 20230918: 重新tuì下kàu頂起做parser. add rule + - 20230921-22:add rule, report issue diff --git a/lexicalSyntaxesDraft.pdf b/lexicalSyntaxesDraft.pdf new file mode 100644 index 0000000..33983e8 Binary files /dev/null and b/lexicalSyntaxesDraft.pdf differ diff --git a/parserRule.txt b/parserRule.txt index 0bce539..8b2b5de 100644 --- a/parserRule.txt +++ b/parserRule.txt @@ -1 +1,5 @@ -expr = int | int add int # expr1 and #expr2 \ No newline at end of file +single = "(" expr ")" | int +fac1 = applier "(" int ")" | single +applier = expr +term = fac | fac (MUL | DIV) fac +expr = term (ADD | SUB) term diff --git a/src/index.js b/src/index.js index 0e3686c..4163e92 100644 --- a/src/index.js +++ b/src/index.js @@ -101,6 +101,7 @@ let tMul = m1TType(tk.TokenType.I_MUL); let tDiv = m1TType(tk.TokenType.I_DIV); let tLParen = m1TType(tk.TokenType.L_PAREN); let tRParen = m1TType(tk.TokenType.R_PAREN); +let toSome = tk.toSome; node_process_1.argv.forEach((val, index) => { console.log(`${index}=${val}`); }); @@ -160,17 +161,27 @@ let circumfix = (f, signal) => (x) => { var a = f(x); if (a._tag == "Some") { let inner = a.value.ast[a.value.ast.length - 2]; - console.log("AST====" + repr(a.value.ast)); let ast_middle = [inner]; let new_ast = [ast_middle]; a.value.ast = new_ast; - console.log("+" + signal + "+" + repr(a)); } return a; }; -/** fac1 = "(" expr ")" */ -let fac1 = circumfix((x) => thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1"); -let fac2 = tInt; +/** + * TODO: 12(13)(14) only parsed with only 12(13) + */ +/** 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); /** * @@ -199,7 +210,9 @@ let expr2 = term; * expr = 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) => { return (x.type != tk.TokenType.NL && x.type != tk.TokenType.SP); @@ -214,4 +227,4 @@ let beta = expr({ remained: tokensFiltered, ast: [] }); -console.log(repr(beta)); +console.log("RESULT=" + repr(beta)); diff --git a/src/index.ts b/src/index.ts index acaa74e..0d94c74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -101,6 +101,7 @@ let tDiv = m1TType(tk.TokenType.I_DIV); let tLParen = m1TType(tk.TokenType.L_PAREN); let tRParen = m1TType(tk.TokenType.R_PAREN); +let toSome = tk.toSome; argv.forEach((val, index) => { @@ -170,28 +171,36 @@ let circumfix = (f : Function, signal? : string) => (x : TokenMatcheePair)=>{ var a = f(x); if (a._tag == "Some"){ let inner = a.value.ast[a.value.ast.length-2]; - console.log("AST===="+repr(a.value.ast)); let ast_middle : tkTree[] = [inner]; let new_ast = [ast_middle]; a.value.ast = new_ast; - - console.log("+"+signal+"+"+repr(a)); - - } return a; } -/** fac1 = "(" expr ")" */ -let fac1 = circumfix((x : TokenMatcheePair)=> -thenDo(thenDo(thenDo(tk.toSome(x), tLParen), expr), tRParen), "fac1"); - -let fac2 = tInt; - +/** + * 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"); +let single2= tInt; +let single = orDo(single1, single2); + +/** 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); - /** * * 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( (x)=>{return (x.type != tk.TokenType.NL && x.type != tk.TokenType.SP)}); @@ -249,5 +260,5 @@ let beta = expr({ ast : []}); -console.log(repr(beta)); +console.log("RESULT="+repr(beta));