Can you help me with this example? How should i do left recursion elimination with this size? i know how to do it for simpler examples.
Expr1 ::= Number
| String
| `true`
| `false`
| `undefined`
| Expr1 `+` Expr1
| Expr1 `-` Expr1
| Expr1 `*` Expr1
| Expr1 `%` Expr1
| Expr1 `<` Expr1
| Expr1 `===` Expr1
| Ident AfterIdent
| `[` Exprs `]`
| `[` `for` `(` Ident `of` Expr `)` ArrayCompr Expr `]`
| `(` Expr `)`
Is this the solution?
Expr1 ::= Number ExprB
| String ExprB
| `true` ExprB
| `false` ExprB
| `undefined` ExprB
| Ident AfterIdent ExprB
| `[` Exprs `]`
| `[` `for` `(` Ident `of` Expr `)` ArrayCompr Expr `]`
| `(` Expr `)`
ExprB ::= ϵ
| `+` Expr1 ExprB
| `-` Expr1 ExprB
| `*` Expr1 ExprB
| `%` Expr1 ExprB
| `<` Expr1 ExprB
| `===` Expr1 ExprB
The trick I've learned is to introduce constructive non-terminals to get fewer grammar rules at any one spot. You'll still have some nasty expansions inherent in the language, but you can make the process easier at each step.
Scalar ::= Number | String | `true` | `false` | `undefined`
Op ::= '+' | '-' | '*' | '%' | '<' | '==='
OpExpr ::= Expr1 Op Expr1
ParenExpr ::=
`[` Exprs `]`
| `[` `for` `(` Ident `of` Expr `)` ArrayCompr Expr `]`
| `(` Expr `)`
Expr1 ::=
Scalar
| OpExpr
| ParenExpr
| Ident AfterIdent
There are two main gains here. One is that if you're implementing the parser, the rules now more closely match the families of processing. You can take certain common actions upon the classified reductions. The second is that you can simplify your elimination of recursion: you have the same quantity of terminals to begin an Expr1, but only one rule to expand, the definition of OpExpr.
I know that I haven't completed the exercise for you, but I hope that this helps enough to get you moving. You might also want to check out operator precedence grammars: they handle some of this in elegant fashion, depending on your application.
Related
//Expression
exp: exp1 ASS_OP exp | exp1;
exp1: exp1 OR_OP exp2 | exp2;
exp2: exp2 AND_OP exp3 | exp3;
exp3: exp4 (EQUAL_OP | NOT_EQUAL_OP) exp4 | exp4;
exp4: exp5 (LESS_OP|GREATER_OP|LESS_EQUAL_OP|GREATER_EQUAL_OP) exp5 | exp5;
exp5: exp5 (ADD_OP | SUB_OP) exp6 | exp6;
exp6: exp6 (MUL_OP | DIV_OP | MOD_OP) exp7 | exp7;
exp7: (ADD_OP | SUB_OP | NOT_OP) exp7 | exp8;
exp8: LB exp RB | expl;
expl: invocation_exp | index_exp | ID | INTLIT |FLOATLIT | BOOLEANLIT | STRINGLIT;
index_exp: exp LSB exp RSB;
invocation_exp: ID LB (exp (COMMA exp)*)? RB;
[error] error(119): MC.g4::: The following sets of rules are mutually left-recursive [exp, index_exp, exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, expl]
[trace] Stack trace suppressed: run last *:Antlr generates lexer and parser for the full output.
Hi, I'm new. I read some topics so that ANTLR4 supports only direct left-recursion. And authors seem don't want to change that. So anyone can help me fix my code? Thank for reading this.
One of the great things about ANTLR4 as opposed to some of the older tools is that you don't often have to "chain" your operator precedence like this:
exp: exp1 ASS_OP exp | exp1;
exp1: exp1 OR_OP exp2 | exp2;
exp2: exp2 AND_OP exp3 | exp3;
I remember those days of chaining expression rules for strict BNF grammars. But in ANTLR4, we can do better and have a clearer grammar too. Since the rules are evaluated top to bottom, the highest-predecence rules are listed first like in this snippet:
expr
: expr POW<assoc=right> expr #powExpr
| MINUS expr #unaryMinusExpr
| NOT expr #notExpr
| expr op=(MULT | DIV | MOD) expr #multiplicationExpr
| expr op=(PLUS | MINUS) expr #additiveExpr
| expr op=(LTEQ | GTEQ | LT | GT) expr #relationalExpr
| expr op=(EQ | NEQ) expr #equalityExpr
| expr AND expr #andExpr
| expr OR expr #orExpr
| atom #atomExpr
This might solve your operator precedence issues without wrangling with mutual left recursion. Highest precedence at top, with power (exponentiation) in this example (and by mathematical convention) having the highest binding in an expression.
I'm using jq 1.4 and am confused about the following situation. I can calculate a number, but get an error when I try to construct an object with this number:
echo '{"aggregations":{"sent":{"value":25},"bounced":{"value":null},"incoming_act":{"value":25}}}' |
jq '.aggregations
| {"num_sent": .sent.value, "num_incoming_act": .incoming_act.value }
| .num_sent as $x
| .num_incoming_act as $y
| $y-$x as $d
| $d'
0
works fine. But
echo '{"aggregations":{"sent":{"value":25},"bounced":{"value":null},"incoming_act":{"value":25}}}' |
jq '.aggregations
| {"num_sent": .sent.value, "num_incoming_act": .incoming_act.value }
| .num_sent as $x
| .num_incoming_act as $y
| $y-$x as $d
| {diff: $d}'
jq: error: number and object cannot be subtracted
doesn't work. Same happens when I ask for objects in the last part:
echo '{"aggregations":{"sent":{"value":25},"bounced":{"value":null},"incoming_act":{"value":25}}}' |
jq '.aggregations
| {"num_sent": .sent.value, "num_incoming_act": .incoming_act.value }
| .num_sent as $x
| .num_incoming_act as $y
| $y-$x as $d
| objects'
jq: error: number and object cannot be subtracted
I love jq's pipe system. However, something seems to be going on here. What is the "0" that I get in the first example? It doesn't seem to be a normal number 0. This works again:
jq -n ' 0 as $x | {diff: $x} '
This
echo '{"aggregations":{"sent":{"value":25},"bounced":{"value":null},"incoming_act":{"value":12}}}' | jq '.aggregations | {"num_sent": .sent.value, "num_incoming_act": .incoming_act.value } | {diff:(.num_sent as $x | .num_incoming_act as $y | $y-$x as $d | $d)}'
Will Produce:
{
"diff": -13
}
Difference being here;
Previous: .num_sent as $x | .num_incoming_act as $y | $y-$x as $d | {diff: $d}'
Now: {diff:(.num_sent as $x | .num_incoming_act as $y | $y-$x as $d | $d)}'
You can probably see by visualising the difference, where jq is processing things.
In the examples where you get an error, write ($y-$x) as $d rather than just $y-$x as $d. The parentheses are sometimes necessary, and always advisable, when writing (COMPOUND INFIX EXPRESSION) as $variable.
Explanation:
The parser treats expressions of the form:
3-2 as $d | EXPR
as:
3-(2 as $d | EXPR)
This means that 3-2 as $d|$d is parsed as 3-(2 as $d|$d) which evaluates to 3-2. Notice, though, that in this case, $d itself has the value 2.
How do I denormalize this table
+---------+---------------------------------------------+
| lemma | definition |
+---------+---------------------------------------------+
| abandon | a feeling of emotional intensity |
| abandon | forsake |
| abandon | leave behind |
| abbey | a church associated with a monastery |
| abbey | a convent ruled by an abbess |
+---------+---------------------------------------------+
as
+---------+-------------------------------------------------------------------------------+
| lemma | definition |
+---------+-------------------------------------------------------------------------------+
| abandon | a feeling of emotional intensity forsake leave behind |
| abbey | a church associated with a monastery a convent ruled by an abbess |
+---------+-------------------------------------------------------------------------------+
What should be the query?
SELECT lemma, GROUP_CONCAT(definition, ' ') AS definition
FROM your_table
GROUP BY lemma
GROUP_CONCAT is one of the aggregate functions available in SQLite
Try this:
SELECT lemma, GROUP_CONCAT(definition,' ')
FROM your_table
GROUP BY lemma
Works in SQLite.
GROUP_CONCAT function
As this link says:
The group_concat() function returns a string which is the
concatenation of all non-NULL values of X. If parameter Y is present
then it is used as the separator between instances of X. A comma (",")
is used as the separator if Y is omitted. The order of the
concatenated elements is arbitrary.
I have written the following BNF "code", which attempts to describe simple mathematics using BNF. The issue I am having is that I have no idea how to add parentheses (brackets).
Digit ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";
Digits ::= <Digit>|<Digit><Digit>;
Number ::= <Digits>|<Digits>.<Digits>;
Addition ::= <Value> + <Value>;
Subtraction ::= <Value> - <Value>;
Multiplication ::= <Value> * <Value>;
Division ::= <Value> / <Value>;
Value ::= <Number>|<Addition>|<Subtraction>|<Multiplication>|<Division>;
The other issue is that I'm not sure that the BNF is 100% correct, as the Value "description" doesn't look right to me.
Digit ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";
Digits ::= <Digit>|<Digit><Digits>;
Number ::= <Digits>|<Digits>.<Digits>;
Operator ::= "+" | "-" | "*" | "/"
Bracket_Left ::= "("
Bracket_Right ::= ")"
Value ::= <Number>|<Bracket_Left><Value><Bracket_Right>|<Value><Operator><Value>
Maybe not the most elegant solution, but should work. Always keep in mind the power of recursion.
If you are after operator precedence too, you should use well known method by a recursion (right one in my example):
AddSub ::= <MulDiv> ("+" | "-") <AddSub> | <MulDiv>;
MulDiv ::= <Brackets> ("*" | "/") <MulDiv> | <Brackets>;
Brackets ::= "(" <AddSub> ")" | <Decimal>;
Decimal ::= <Integer> "." <Integer> | <Integer>;
Integer ::= <Digit> <Integer> | <Digit>;
Digit ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";
and operator precedence is automatically followed by parser, without further intervention. I didn't invent this method, it is there for decades, but I have to admit it's kind of genial.
I need to convert this from EBNF to BNF.
<statement> ::= <ident> = <expr>
<statement> ::= IF <expr> THEN <statement> [ ELSE <statement> ] END
<statement> ::= WHILE <expr> DO <statement> END
<statement> ::= BEGIN <statement> {; <statement>} END
Also, I'm stuck on this one:
E -> E+T | E-T | T
T -> T*F | T/F | F
F -> (E) | VAR | INT
VAR -> a | b | c
INT -> 0 | 1 | 2| 3 | 4| 5 | 6 | 7 | 8 | 9
After modifying the grammer to add a ^ operator, What is the leftmost derivation that your grammar assigns to the expression a^2^b*(c+1)? You may find it convenient to sketch the parse tree for this expression first, and then figure out the leftmost derivation from that.
I added G -> F^G | G and then got G 2 G b E as my answer but am not sure if that is correct.