I'm looking for Ruby grammar in BNF form. Is there an official version?
The YACC syntax is in the Ruby source. Download it and run the bundled utiliy to get the readable syntax.
wget ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p195.tar.gz
tar xvzf ruby-2.0.0-p195.tar.gz
cd ruby-2.0.0-p195
ruby sample/exyacc.rb < parse.y
Output sample (total 918 lines for the v2.0.0-p195)
program : top_compstmt
;
top_compstmt : top_stmts opt_terms
;
top_stmts : none
| top_stmt
| top_stmts terms top_stmt
| error top_stmt
;
top_stmt : stmt
| keyword_BEGIN
'{' top_compstmt '}'
;
bodystmt : compstmt
opt_rescue
opt_else
opt_ensure
;
compstmt : stmts opt_terms
;
Yes, there is one Ruby BNF syntax by the University of buffalo.
Edit: I've also found this alternate Ruby BNF syntax.
also an official version: Ruby Draft Specification. you can find the grammar there.
Ruby Draft Specification: http://ruby-std.netlab.jp. the server is down, but you can download it from
http://www.ipa.go.jp/osc/english/ruby
Related
I am running into a performance issue while creating the grammar for Xpath.
The whole grammar was working fine till we added support for the xpaths like:
((div)[1]//span)[1]
or
((//div)[1]/div)[last()]
After adding support for this in the grammar file, the above xpaths started working fine but other xpaths started giving performance issues.
Like this one :
//label[normalize-space(.)='Phone']/parent::lightning-input/parent::slot/parent::slot/parent::span/parent::div/parent::force-record-layout-item/parent::slot/parent::force-record-layout-row/parent::slot/parent::div/parent::div/parent::div/parent::force-record-layout-section/parent::slot/parent::force-record-layout-block/parent::forcegenerated-detailpanel_contact___012b0000000jhhvia4___full___view___recordlayout2/parent::records-lwc-record-layout/parent::slot/parent::records-record-layout-event-broker/parent::div/parent::div/following-sibling::force-form-footer//button
which started taking 30 seconds in parsing (it took 170ms earlier).
These were the lines of code added/modified in the attached grammar file which started giving performance issue:
union
: expressions+=pathExpression (WS? operator='|' WS? expressions+=pathExpression)*
;
pathExpression
: expressionAtom
| expressionAtom nodeSet
;
expressionAtom
: functionCall
| nodeSet
| literal
| parenthesis
;
Earlier it was:
union
: expressions+=expressionAtom (WS? operator='|' WS? expressions+=expressionAtom)*
;
expressionAtom
: functionCall
| nodeSet
| literal
| parenthesis
;
and used to give no performance issues.
On debugging the XpathParser I figured out that it is because the DFAState has** requiresFullContext as true **in this case.
The documentation states that the true value of this “Indicates that this state was created during SLL prediction that discovered a conflict between the configurations in the state.”
Can you help me resolve this issue and what is causing it to search the full context which takes time?
I don't know Antlr well, but how about replacing
pathExpression
: expressionAtom
| expressionAtom nodeSet
;
with
pathExpression
: expressionAtom nodeSet?
;
As the title suggest, my prolog code is throwing a syntax error. Im not sure what Im doing wrong. Im using Swi for my IDE and I tried playing with it to fix the problem, but to no avail.
heres my simple prolog code with error
?-
| male(bob)
| male(jeff)
|
| female(jane)
| female(erica)
|
| father(bob,jane)
| mother(erica, jane)
|
| ?-mother(erica,X).
ERROR: Syntax error: Operator expected
ERROR: male(bob)
ERROR: ** here **
ERROR:
male(jeff)
female(jane)
female(erica)
father(bob,jane)
mother(erica, jane)
?-mother(erica,X) .
There are two phases of Prolog development: Writing the program and interacting with it in the Prolog shell. These two phases are separate. You don't write your program in the shell, at least not directly.
Save your facts in a file called family.pl (with a dot . at the end of each fact!), then start the Prolog shell. In the shell, you can load the program using
?- consult(family).
or
?- consult('family.pl').
Note that in the first case you leave off the .pl extension, but in the second case, if you do use the extension, you should use single quotes (') around the file name.
Now you can run your query:
?- mother(erica, X).
X = jane.
There are some other ways to load files, such as putting the file name between square brackets [] instead of using consult, or (for many Prolog systems) simply adding the file name on the command line.
Statements in prolog end with a dot:
male(jeff).
female(jane).
female(erica).
father(bob,jane).
mother(erica, jane).
We had a test that found every Ruby file in our application and ran ruby -c on it. We introduced Rubocop and made it check the same list of files.
Is the test that ran ruby -c actually now useless, or is there an example of a failure mode that would be caught by ruby -c but not Rubocop?
The documentation for ruby -c says:
Causes Ruby to check the syntax of the script and exit without executing. If there are no syntax errors, Ruby will print "Syntax OK" to the standard output.
This is an example of a syntax issue that will be caught by either:
% echo "puts 'hello world" > hot_script.rb
% ruby -c hot_script.rb
hot_script.rb:1: unterminated string meets end of file
% rubocop hot_script.rb
Inspecting 1 file
F
Offenses:
hot_script.rb:1:6: F: unterminated string meets end of file
(Using Ruby 1.9 parser; configure using TargetRubyVersion parameter, under AllCops)
puts 'hello world
^
1 file inspected, 1 offense detected
Rubocop even catches some of the same warnings, though I didn't have ruby -c configured to catch these previously, and I am therefore more interested in errors. Here's an example of relative parity in handling a warning:
% cat unused_var.rb
def hot_method
a = 1
b = 2
puts b
end
% ruby -cwW2 unused_var.rb
unused_var.rb:2: warning: assigned but unused variable - a
Syntax OK
% rubocop unused_var.rb
Inspecting 1 file
W
Offenses:
unused_var.rb:2:3: W: Lint/UselessAssignment: Useless assignment to variable - a.
a = 1
^
1 file inspected, 1 offense detected
I searched using
https://www.google.com/search?q=rubocop%20syntax%20check%20superset
https://www.google.com/search?q=is%20there%20any%20reason%20to%20run%20ruby%20-c%20syntax%20check%20if%20i%20use%20rubocop
but I may be doing it wrong. The test is way slower in Ruby 1.9 than it was in Ruby 1.8, so the answer to this question is actually valuable to me. And you have to admit, you're curious, right?
The answer is "most of the time." RuboCop builds on the parser gem, which is a standalone Ruby parser which mimics, more or less, the MRI parser. RuboCop wraps parser's syntax checks and will properly report issues. However, as stated on the parser's GitHub:
Unfortunately MRI often changes syntax in patch level versions
[...] there is no simple way to track these changes.
This policy makes it all but impossible to make Parser precisely
compatible with the Ruby MRI parser.
In addition, parser supports the latest minor version of whatever release you are using, and doesn't backport minor versions. So if you use Ruby 2.4.0, RuboCop will use a parser version supporting 2.4.1 syntax.
For all intents and purposes, parser is equivalent to the official MRI parser, and unless you have a specific reason to use both, using RuboCop alone should be sufficient.
Rubocop will also identify and report syntax errors since the code cannot be properly parsed if that is the case, so there's no need for both.
I know there is the Swift REPL and the Xcode playgrounds, but I wonder whether there is an alternative to ruby -e "<code>" or sh -c "<code>" in Swift where the given one line code would be executed as the result of the command?
There isn't a direct equivalent (you can ask the swift command for its options with swift --help and see there's nothing like Ruby's -e).
But there's a workaround.
You can pass a Swift expression to the compiler direclty using echo and | (the "pipe") like this:
echo "print(42)" | swift
Result:
Welcome to Apple Swift version 2.1.1 ("700.1.101.9 700.1.78"). Type :help for assistance.
42
I guess it's similar to the behavior you were looking for.
We notice that it always prints the introduction sentence, but there's a way to fix this, by adding - at the end of the command, like this:
echo "print(42)" | swift -
Result:
42
When using literal strings, escape the double quotes:
echo "print(\"hello\")" | swift -
Result:
hello
You can execute any expression, even loops:
echo "for num in 1...5 { print(num) }" | swift -
Result:
1
2
3
4
5
etc.
It's still the REPL so it will give feedback about variables (omitting the - trick at the end), for example:
echo "let x = 42;print(x)" | swift
Result:
Welcome to Apple Swift version 2.1.1 ("700.1.101.9 700.1.78"). Type :help for assistance.
42
x: Int = 42
I found in Ruby's documentation that C-style comments are supported:
You can insert comment about all places. Two style comment can be used, Ruby style (#.….) and C style ( .….. ) .
Does this mean that Ruby supports /* and */? When I try it, it comes with an error:
target of repeat operator is not specified: /*
test
*/
That is not a grammar for Ruby, but a grammar for Racc (a Ruby equivalent of yacc). Just like yacc files are not C, Racc files are not Ruby.