Transform grammar to avoid common prefixes - javacc

I have this grammar with common prefixes (<id>) and I want to transform it to avoid them.
void Components() : {}
{
(Read() | Write())* (<id>Assignment())* <id>Declaration() (Read() | Write() | <id>(Assignment() | Declaration()))*
}
The problem is (<id>Assignment())* <id>Declaration(). The grammar can have 0 or more Assignments/Read/Write statments but at least 1 Declaration and then any statment/declaration in any order.

Refactoring this is easy, but I probably wouldn't do it. I'd probably look ahead a little further. Here are two solutions
Factor out the <id>
void Components() : {}
{
(Read() | Write())*
<id>
(Assignment() <id>)*
Declaration()
( Read()
| Write()
| <id> (Assignment() | Declaration())
)*
}
Use longer lookahead
void Components() : {}
{
(Read() | Write())*
(LOOKAHEAD( 2 ) <id> Assignment())*
<id> Declaration()
( Read()
| Write()
| LOOKAHEAD( 2 ) <id> Assignment()
| <id> Declaration())
)*
}

Related

jq — Extract strings contained in nested arrays and objects

I have this jq filter and input:
( ._birthDate.extension[0] | .url, ( .extension[0] | .url, .valueString ), ( .extension[1] | .url, .valueString ) )
{
"_birthDate":{
"extension":[
{
"url":"http://catsalut.gencat.cat/fhir/StructureDefinition/patient-dataBirhtDeath",
"extension":[
{
"url":"country",
"valueString":"724"
},
{
"url":"state",
"valueString":"08"
}
]
}
]
}
}
…which yields the following output:
"http://catsalut.gencat.cat/fhir/StructureDefinition/patient-dataBirhtDeath"
"country"
"724"
"state"
"08"
I wanted to refactor the filter:
( ._birthDate.extension[0] | .url, ( .extension[:2] | .url, .valueString ) )
…but I am getting the following error:
jq: error (at :18): Cannot index array with string "url"
See this demo.
Array/String Slice: .[10:15] [docs]
... Either index may be negative (in which case it counts backwards from the end of the array), or omitted (in which case it refers to the start or end of the array).
So your were first using .extension[0] that ment: take index 0 from .extension where
.extension[:2] means: take index 0 up and including index 2 from .extension
As #pmf already mentiond, the difference is the returned value, an object at first, but an array on the second.
So you can loop over the array using [] to return the .url and .valueString for each object in side the .extension array:
._birthDate.extension[0] | .url, ( .extension[:2][] | .url, .valueString )
Online Demo
However, since .extension is an array with only 2 indexes, the :2 part doesn't do anything useful in your example, so why not simplify it to:
._birthDate.extension[0] | .url, ( .extension[] | .url, .valueString )
Online Demo
If you only need to keep the strings then you can use .. to recursively traverse your document and strings to filter out non-strings yielded along the way:
.. | strings
Demo

TypeScript signature for a recursive Flatten Function

I'm working on building a Typescript .ds file for reactive-coffee (http://yang.github.io/reactive-coffee/api.html), and I'm running into trouble trying to figure out the type signature for the flatten function. An example:
flatten(rx.array([1, [2, 3], rx.cell(4), rx.array([rx.cell([5, rx.cell(6)])])]))
// -> [1,2,3,4,5,6]
flatten([1, [2, 3], rx.cell(4), rx.array([rx.cell([5, rx.cell(6)])])])
// -> [1,2,3,4,5,6]
The question I'm running into trouble with is: what is the correct Typescript type signature for xs? So far I've come up with something like this:
interface NestableCell<T> extends ObsCellInterface<T | NestableCell<T>> {}
type Flattenable<T> = (
Array<T| NestableCell<T | Flattenable<T>> | Flattenable<T>> |
ObsArrayInterface<T | NestableCell<T | Flattenable<T>> | Flattenable<T>>
)
function flatten<T>(xs:Flattenable<T>) => ObsArrayInterface<T>
ObsCellInterface and ObsArrayInterface are typed versions of RC's ObsCell and ObsArray objects, respectively.
Unfortunately, Typescript does not allow recursive types, only recursive interfaces. And at this point I'm really not sure how to convert that type to an interface.
The following seems to work, though I haven't yet had time to prove that it satisfies all possible cases:
interface NestableCell<T> extends ObsCell<T | NestableCell<T>> {}
interface FlattenableRX<T> extends ObsArray<
T |
NestableCell<T | FlattenableJS<T> | FlattenableRX<T>> |
FlattenableJS<T> |
FlattenableRX<T>
> {}
interface FlattenableJS<T> extends Array<
T |
NestableCell<T | FlattenableJS<T> | FlattenableRX<T>> |
FlattenableJS<T> |
FlattenableRX<T>
> {}
export type Flattenable<T> = FlattenableRX<T> | FlattenableJS<T>
Using two mutually recursive interfaces appears to avoid the worst complications from having to support both primitive and reactive arrays.
As I said, I cannot yet prove that this works, but it at least seems plausible.

How to solve this grammar recursion?

I found this grammar for a calculator:
<Expression> ::= <ExpressionGroup> | <BinaryExpression> | <UnaryExpression> | <LiteralExpression>
<ExpressionGroup> ::= '(' <Expression> ')'
<BinaryExpression> ::= <Expression> <BinaryOperator> <Expression>
<UnaryExpression> ::= <UnaryOperator> <Expression>
<LiteralExpression> ::= <RealLiteral> | <IntegerLiteral>
<BinaryOperator> ::= '+' | '-' | '/' | '*'
<UnaryOperator> ::= '+' | '-'
<RealLiteral> ::= <IntegerLiteral> '.' | <IntegerLiteral> '.' <IntegerLiteral>
<IntegerLiteral> ::= <Digit> <IntegerLiteral> | <Digit>
<Digit> ::= '0' | '1' |'2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Source: here
It looks great. So I wrote the lexer and started the parser. Now there is an infinite recursion that I can't solve between Expression and BinaryExpression.
My code for expression:
boolean isExpression() {
if (isExpressionGroup() || isBinaryExpression() || isUnaryExpression() || isLiteralExpression()) {
println("Expression!");
return true;
}
println("Not expression.");
return false;
}
And for binary expression:
boolean isBinaryExpression() {
if (isExpression()) {
peek(1);
if (currentLex.token == Token.BINARY_OPERATOR) {
peek(2);
if (isExpression()) {
peek(3);
println("Binary expression!");
return true;
} else peek(0);
} else peek(0);
} else peek(0);
return false;
}
So peek(int) is just a function for looking forward without consuming any lexemes. So my problem: My input is '2*3' . isExpression() gets called. isExpressionGroup() fails, because there is no '('. Then the isBinaryExpression() gets called, which calls isExpression(). isExpressionGroup() fails again, and isBinaryExpression() gets called again. And so on, until a stack overflow.
I know, there is ANTLR and JavaCC (and other tools), but I would like to do it without them.
Could anyone give a hand?
Dealing with left recursion in a hand-crafted top-descent parser is not easy. Parser generators that solve the problem have years of work in them. There are theoretical reasons for that.
The best solution if you don't want to use a tool is to eliminate the left recursion. The problem if you do it "by the book" is that you'll get an ugly grammar and an ugly parser that will be difficult to use.
But there's another solution. You can add enough rules to represent the precedence hierarchy of the operators, which is something you'd have to do anyway, unless you want to risk a a+b*c be parsed as (a+b)*c.
There are plenty of examples of non left-recursive grammars for expressions on the Web, and here in SO in particular. I suggest you take one of them, and start from there.

Find ultimate parent of an entity using recursion in c#

I have an Entity which in turn refers to same table which is its parent. Below is the table which describes it more better.
| ID | Source_ID |
+----+----------+
| 1 | null |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 4 |
Now, when I am at ID = 5, I need to fetch its ultimate parent, which is ID = 1.
I tried writing a function which is as below:
<entity> ultimateparententity;
internal <entity> FetchParentComponentRecursive(<entity> entity)
{
if (component.ParentEntity!= null)
{
FetchParentComponentRecursive(entity.ParentEntity);
}
else
{
ultimateparententity = entity;
return component;
}
return entity;
}
I am using variable declared at class level to know the ultimate parent. I am returning variable "Entity" which is never used later, but ultimateparententity is what is used. This approach works, but I am not too happy with this. Any directions will be helpful.
I'm not too familiar with C#, but the general structure of your recursive function looks off.
Try something along the lines of:
internal <entity> FetchParentComponentRecursive(<entity> entity)
{
if (component.ParentEntity == null)
{
return component;
}
else
{
return FetchParentComponentRecursive(entity.ParentEntity);
}
}
By the way, this very much depends on there being no circular references in your data set.

String pointers

Consider the following program (http://play.golang.org/p/IbAstvudtE):
package main
import (
"fmt"
)
func changeStringValueNotOK(dest *string, src string) {
dest = &src
}
func changeStringValueOK(dest *string, src string) {
*dest = src
}
func main() {
a := "Hello"
b := "World"
changeStringValueNotOK(&a, b)
fmt.Println(a) // still "Hello"
changeStringValueOK(&a, b)
fmt.Println(a) // now "World"
}
My goal is to call a function and change the value of string. Works fine with the second function, but not with the first.
Question: what is the meaning of *dest = src compared to dest = &src? I guess the former is "the contents of dest is now src" and the latter is "change the dest variable so that it now points to the address of src" which discards the previous value, but not the contents of a. But even if I am right, I don't understand how the *dest = src works.
I hope my question isn't too fuzzy.
*dest = src
is: Set the value pointed at by dest to the value in src. So it's effective.
dest = &src
is: Set the value of dest to the address of src. As dest is a formal parameter of changeStringValueNotOK the change (to the pointer only, not to the pointee) is visible only locally. As the changed value is not really used, it's total effect is a no operation.
Just to help visualize it graphically, in your main function you have this:
Data Variables
| ... |
-------
| Hello |<----- <a string>
-------
| World |<----- <b string>
-------
| ... |
When you provide the parameters to either of these functions, it becomes:
Data Parameters
| ... |
-------
| Hello |<----- <a string> <----- <dest *string>
-------
| World |<----------------------- <src string>
-------
| ... |
With that scenario, when you do *dest = src, you get:
Data Parameters
| ... |
-------
| Hello | ,- <a string> <---- <dest *string>
------- |
| World |<--/
| |<---------------------- <src string>
-------
| ... |
Note that this effectively changes a itself, so when you return to the prior scope, you'll observe the change.
On the other hand, if you do dest = &src, you'd instead get:
Data Parameters
| ... |
-------
| Hello | <dest *string> ----,
------- |
| World |<---------------------- <src string> <---/
-------
| ... |
Which doesn't solve your problem, because both dest and src are local to the functions they're in.
As a side note, this kind of parameter passing is a common C idiom, but in Go you'd generally change such a string by returning the new value instead.

Resources