I would like to parse an expression with parenthesis in python using textx.
For example the following DSL :
CREATE boby = sacha - ( boby & tralaa) ;
CREATE boby = sacha & boby - ( david & lucas )
This is the grammar I tried:
Model:
'CREATE' name=Identifier '=' exp=SetExpr
;
JoinOperator: /-/&/;
SetExpr:SetParExpr | SetBaseExpr
;
SetBaseExpr:
first=ID op=JoinOperator second=ID
;
SetParExpr:
'(' SetExpr ')'
I guess I should have a list somewhere to fill with expression.
Do you have any suggestion ?
I've changed your examples just slightly: I added a semicolon to end and I put another pair of parentheses in your second example. I inferred these changes based on what you provided in your grammar. Here's the examples:
CREATE boby = sacha - ( boby & tralaa);
CREATE boby = sacha & (boby - ( david & lucas ));
To parse examples like these your grammar needs to be changed to:
Take in multiple Models (I created a Script rule that takes semi colon separated models)
Allow the second property of the SetBaseExpr rule to be an ID or a SetParExpr.
Change Identifier to ID in the model rule (I assume this is what you meant).
I made these changes and ended up with the following grammar that parses the examples I gave:
Script:
models+=Model[';'] ';'
;
Model:
'CREATE' name=ID '=' exp=SetExpr
;
JoinOperator: '-' | '&';
SetExpr:
SetParExpr | SetBaseExpr
;
SetBaseExpr:
first=ID op=JoinOperator (second=ID | second=SetParExpr)
;
SetParExpr:
'(' SetExpr ')'
;
I hope that answers your question or gives you a hint as to handle parenthetical expressions.
Related
I have a grammar with command lines starting with a / and "data lines" which is everything that does not start with a slash.
I just can't get it to be parsed correctly, the following rule
FM_DATA: ( ('\r' | '\n' | '\r\n') ~'/') -> mode(DATA_MODE);
does almost what I need but for a data line of
abcde
the following tokens are generated
[#23,170:171='\na',<4>,4:72]
[#24,172:175='bcde',<103>,5:1]
so the first character is swallowed by the rule.
I also tried
FM_DATA: ( {getCharPositionInLine() == 0}? ~'/') -> mode(DATA_MODE);
but this causes even weirder things.
What's the correct rule for getting this to work as expected ?
TIA - Alex
The ... -> more command can be used to let the first char (or first part of a lexer rule) not be consumed (yet).
A quick demo:
lexer grammar FmDataLexer;
NewLine
: [\r\n]+ -> skip
;
CommandStart
: '/' -> pushMode(CommandMode)
;
FmDataStart
: . -> more, pushMode(FmDataMode)
;
mode CommandMode;
CommandLine
: ~[\r\n]+ -> popMode
;
mode FmDataMode;
FmData
: ~[\r\n]+ -> popMode
;
If you run the following code:
FmDataLexer lexer = new FmDataLexer(CharStreams.fromString("abcde\n/mu"));
CommonTokenStream stream = new CommonTokenStream(lexer);
stream.fill();
for (Token t : stream.getTokens()) {
System.out.printf("%-20s '%s'\n", FmDataLexer.VOCABULARY.getSymbolicName(t.getType()), t.getText());
}
you'll get this output:
FmData 'abcde'
CommandStart '/'
CommandLine 'mu'
EOF '<EOF>'
See: https://github.com/antlr/antlr4/blob/master/doc/lexer-rules.md#mode-pushmode-popmode-and-more
I'm looking for the correct syntax to add some (") between my variable.
I need something like that :
"firstname","lastname","email","",""
Here is the first script I have :
foreach($line in Get-Content .\extract.csv)
{ $firstname = $line.split(';')[0]
$lastname = $line.split(';')[1]
$email = $line.split(';')[2]
$newLine = "$firstname - $lastname - $email"
echo $newLine }
I'm really new in scripting and I'm a bit lost with all these (') (")
My second question is : I need to extract my data only from the second row and ignore the first one, can you help me for this too ?
Thanks !
Have you try escaping your " and ' ?
In powershell you can use backtick ` (AltGr + 7) or doubling the char to do so :
Example :
Write-Host(" `" ")
Write-Host(" "" ")
Please add more code if this doesn't solve you issue !
I have tried text box as form control type and it didn't work for "xxx & xxxx". So looking for suggestions if it is with Form control type or something else.If yes, please do let me know to fix it.
Smart Search (i.e.Lucene) supports escaping special characters that are part of the query syntax. The special characters are: + - && || ! ( ) { } [ ] ^ " ~ * ? : \ /. To escape these characters use the \ before the character. So try "xxx \& xxxx" and see if it works.
thanks #PeterMogilnitski. i have tried using "xxx \& xxxx". It is not working.enter image description here Have a look at this picture. assume Field or searchName is formated as "xxx & xxxx". so what do you suggest for Documentname it should find results for searchname.
I am new to powershell language and I have problems understanding some basic concepts regarding string concatenation.
I tried to concat a string with the + char as I knew it from other programming languages i. e. Java.
line 1: $result = 7
line 2: Write-Host "Result: " + $result + "!" # Result: + 7 + !
I then realized (i. e. in this question How do I concatenate strings and variables in PowerShell?) that I need to do it (in one of) the powershell way(s); for example like this.
line 3: Write-Host "Result: $result!" # Result: 7!
As I experimented a little I found out that if I assign the expression in line 2 to a variable it somehow works as I anticipated it in the first place.
line 4: $str = "Result: " + $result + "!"
line 5: Write-Host $str # Result: 7!
So my question is, why is there a difference if I pass a Java-style concatenated string to Write-Output cmdlet or if I assign the same string to a variable?
String concatenation and expansion is a bit different in PowerShell, here are several ways to accomplish it-
Format operator:
PS C:\> 'This exhibits {0} string expansions! {1} {0}!' -f #(2,'Wow!')
This exhibits 2 string expansions! Wow! 2!
Each item in the array is accessed by the number in the braces.
Subexpression:
PS C:\> "Sometimes you need calculations in a string. 5 + 3 = $(5 + 3)"
Sometimes you need calculations in a string. 5 + 3 = 8
An explanation here: this will not work with string literals, i.e., ''.
Everything contained in the subexpression will be evaluated and converted to a string if possible utilizing an object's ToString() method.
String Expansion:
PS C:\> $Var = 'This string'
PS C:\> "$Var is amazing!"
This string is amazing!
This will also not work with string literals, i.e., ''. If you need to concatenate a variable-qualifying character next to the variable call, you can use curly braces to avoid a null value, i.e., ${Var}_notattached
String Concatenation:
Tried and true:
PS C:\> 'Sometimes you just ' + 'need to add. 8 = ' + 8
Sometimes you just need to add. 8 = 8
Let's say I have this string:
map_data = " *-* ; /|x|\ ; *-*-*-*; /|x|x|x|;-*-*-*-*-*; \|x|x|x|; *-*-*-*; \|x|/ ; *-* ;"
I would like to split the string into an ordered table at the semicolons. Once I have done that I would like to take each element of the table and split each character into an ordered table (nested within the first table). The idea is to create a 2 dimensional table for an ascii map.
I have tried this (but it's not working and I also suspect there is an easier way):
map_data = " *-* ; /|x|\ ; *-*-*-*; /|x|x|x|;-*-*-*-*-*; \|x|x|x|; *-*-*-*; \|x|/ ; *-* ;"
map = {}
p = 1
pp = 1
for i in string.gmatch(map_data, "(.*);") do
map[p] = {}
for ii in string.gmatch(i, ".") do
map[p][pp] = ii
pp = pp + 1
end
p = p + 1
end
To start with, the string map_data is invalid, because \ needs to be escaped. Or you could use the long string syntax [[ ... ]]:
map_data = [[ *-* ; /|x|\ ; *-*-*-*; /|x|x|x|;-*-*-*-*-*; \|x|x|x|; *-*-*-*; \|x|/ ; *-* ;]]
The problem of the pattern (.*); is, the modifier * is greedy. Instead, use - modifier which is lazy:
for i in string.gmatch(map_data, "(.-);") do
It's been years since I've touched Lua but assuming you fix the escape character issue can't you then just do something along the lines of...
map = {{}} -- map initially contains one empty line
for i = 1, #map_data do
local c = map_data:sub(i,i)
if c == ';' then
map[#map+1] = {} -- add another line to the end of map
else
map[#map][ #map[#map] + 1] = c -- add c to last line in map
end
end