Semantic Predicates antlr don't recognize chain of integers of width 4 - antlr3

I need to recognize arrays of integers in Fortran's I4 format (stands for an integer of width four) as the following example:
Using a pure context-free grammar:
WS : ' ' ;
MINUS : '-' ;
DIGIT : '0'..'9' ;
int4:
WS WS (WS| MINUS ) DIGIT
| WS (WS| MINUS ) DIGIT DIGIT
| (WS| MINUS | DIGIT ) DIGIT DIGIT DIGIT
;
numbers
: int4*;
The above example is correctly matched:
However if I use semantic predicates to encode semantic constraints of rule int4 :
int4
scope { int n; }
#init { $int4::n = 0; }
: ( {$int4::n < 3}?=> WS {$int4::n++;} )*
( MINUS {$int4::n++;} )?
( {$int4::n < 4}?=> DIGIT{$int4::n++;} )+
{$int4::n == 4}?
;
it works for the int4 rule, but it's not the same for the numbers rule, because it doesn't recognize the array of integers of the first example:
In this case may be better pure context-free grammar, but in case of the format I30 (stands for an integer of width 30)?
The main question is: Is it possible to use Semantic Predicates with this grammar?

Your parse tree seems to end at the numbers rule because your numbers rule throws an exception (but it does not show up in the diagram...). You can see it if you run the code generated, and if you take a closer look at the exception, it says (line info may differ for you):
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at FortranParser.numbers(FortranParser.java:305)
at Main.main(Main.java:9)
and the code throwing the exception is:
public final void numbers() throws RecognitionException {
....
else if ( (LA5_0==DIGIT) && ((int4_stack.peek().n < 4))) {
alt5=1;
}
So your problem is that the semantic predicate gets propagated to the numbers rule, and at that level the scope stack is empty, hence int4_stack.peek() throws an exception
A trick to avoid it is that you use a variable in the global scope, e.g.:
#members {
int level=0;
}
and modify the semantic predicates to check level before the predicates, just like:
int4
scope { int n; }
#init { $int4::n = 0; level++; }
#after { level--; }
: ( {level==0 || $int4::n < 3}?=> WS {$int4::n++;} )*
( MINUS {$int4::n++;} )?
( {level==0 || $int4::n < 4}?=> DIGIT{$int4::n++;} )+
{$int4::n == 4}?
;
This is just a workaround to avoid the error that you get, maybe (knowing the error) there is a better solution and you don't need to mess up your semantic predicates.
But, I think, the answer is yes, it is possible to use semantic predicates with that grammar.

Related

Lexing The VHDL ' (tick) Token

In VHDL it the ' character can be used to encapsulate a character token ie '.' or it can as an attribute separator (similarish to CPP's :: token) ie string'("hello").
The issue comes up when parsing an attribute name containing a character ie string'('a','b','c'). In this case a naive lexer will incorrectly tokenize the first '(' as a character, and all of the following actual character will be messed up.
There is a thread in comp.lang.vhdl google group from 2007 which asks a similar question
Titled "Lexing the ' char" that has an answer by user diogratia
case '\'': /* IR1045 check */
if ( last_token == DELIM_RIGHT_PAREN ||
last_token == DELIM_RIGHT_BRACKET ||
last_token == KEYWD_ALL ||
last_token == IDENTIFIER_TOKEN ||
last_token == STR_LIT_TOKEN ||
last_token == CHAR_LIT_TOKEN || ! (buff_ptr<BUFSIZ-2) )
token_flag = DELIM_APOSTROPHE;
else if (is_graphic_char(NEXT_CHAR) &&
line_buff[buff_ptr+2] == '\'') { CHARACTER_LITERAL:
buff_ptr+= 3; /* lead,trailing \' and char */
last_token = CHAR_LIT_TOKEN;
token_strlen = 3;
return (last_token);
}
else token_flag = DELIM_APOSTROPHE;
break;
See Issue Report IR1045:
http://www.eda-twiki.org/isac/IRs-VHDL-93/IR1045.txt
As you can see from the above code fragment, the last token can be
captured and used to di"sambiguate something like:
foo <= std_logic_vector'('a','b','c');
without a large look ahead or backtracking.
However, As far as I know, flex doesn't track the last token that was parsed.
Without having to manually keep track of the last parsed token, is there a better way to accomplish this lexing task?
I am using IntelliJ GrammarKit if that helps.
The idea behind IR1045 is to be able to tell whether a single quote/apostrophe is part of a character literal or not without looking ahead or backtracking when you're wrong, try:
library ieee;
use ieee.std_logic_1164.all;
entity foo is
port (
a: in std_logic;
b: out std_logic_vector (3 downto 0)
);
end entity;
architecture behave of foo is
begin
b <= std_logic_vector'('0','1','1','0') when a = '1' else
(others =>'0') when a = '0' else
(others => 'X');
end architecture behave;
How far ahead are you willing to look?
There is however a practical example of flex disambiguation of apostrophes and character literals for VHDL.
Nick Gasson's nvc uses flex, in which he implemented an Issue Report 1045 solution.
See the nvc/src/lexer.l which is licensed under GPLv3.
Search for last_token:
#define TOKEN(t) return (last_token = (t))
and
#define TOKEN_LRM(t, lrm) \
if (standard() < lrm) { \
warn_at(&yylloc, "%s is a reserved word in VHDL-%s", \
yytext, standard_text(lrm)); \
return parse_id(yytext); \
} \
else \
return (last_token = (t));
An added function to check it:
static int resolve_ir1045(void);
static int last_token = -1;
which is:
%%
static int resolve_ir1045(void)
{
// See here for discussion:
// http://www.eda-stds.org/isac/IRs-VHDL-93/IR1045.txt
// The set of tokens that may precede a character literal is
// disjoint from that which may precede a single tick token.
switch (last_token) {
case tRSQUARE:
case tRPAREN:
case tALL:
case tID:
// Cannot be a character literal
return 0;
default:
return 1;
}
}
The IR1045 location has changed since the comp.lang.vhdl post it's now
http://www.eda-twiki.org/isac/IRs-VHDL-93/IR1045.txt
You'll also want to search for resolve_ir1045 in lexer.l.
static int resolve_ir1045(void);
and
{CHAR} { if (resolve_ir1045()) {
yylval.s = strdup(yytext);
TOKEN(tID);
Where we find nvc uses the function to filter detecting the first single quote of a character literal.
This was originally an Ada issue. IR-1045 was never adopted but universally used. There are probably Ada flex lexers that also demonstrate disambiguation.
The requirement to disambiguate is discussed in Ada User Journal volume 27 number 3 from September 2006 in an article Lexical Analysis on PDF pages 30 and 31 (Volume 27 pages 159 and 160) where we see the solution is not well known.
The comment that character literals do not precede a single quote is inaccurate:
entity ir1045 is
end entity;
architecture foo of ir1045 is
begin
THIS_PROCESS:
process
type twovalue is ('0', '1');
subtype string4 is string(1 to 4);
attribute a: string4;
attribute a of '1' : literal is "TRUE";
begin
assert THIS_PROCESS.'1''a /= "TRUE"
report "'1''a /= ""TRUE"" is FALSE";
report "This_PROCESS.'1''a'RIGHT = " &
integer'image(This_PROCESS.'1''a'RIGHT);
wait;
end process;
end architecture;
The first use of an attribute with selected name prefix that has a suffix that is a character literal demonstrates the inaccuracy, the second report statement shows it can matter:
ghdl -a ir1045.vhdl
ghdl -e ir1045
ghdl -r ir1045
ir1045.vhdl:13:9:#0ms:(assertion error): '1''a /= "TRUE" is FALSE
ir1045.vhdl:15:9:#0ms:(report note): This_PROCESS.'1''a'RIGHT = 4
In addition to an attribute name prefix containing a selected name with a character literal suffix there's a requirement that an attribute specification 'decorate' a declared entity (of an entity_class, see IEEE Std 1076-2008 7.2 Attribute specification) in the same declarative region the entity is declared in.
This example is syntactically and semantically valid VHDL. You could note that nvc doesn't allow decorating a named entity with the entity class literal. That's not according to 7.2.
Enumeration literals are declared in type declarations, here type twovalue. An enumerated type that has at least one character literal as an enumeration literal is a character type (5.2.2.1).

Binary operator '/' cannot be applied to two (Int) operands [duplicate]

This question already has answers here:
Passing lists from one function to another in Swift
(2 answers)
Closed 7 years ago.
I am getting a Binary operator '/' cannot be applied to two (Int) operands error when I put the following code in a Swift playground in Xcode.
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf()
sumOf(42, 597, 12)
The above was a function calculating the total sum of any numbers.
Below is a function calculating the average of the numbers. The function is calling the sumOf() function from within itself.
func avg(numbers: Int...) -> Float {
var avg:Float = ( sumOf(numbers) ) / ( numbers.count ) //Binary operator '/' cannot be applied to two (Int) operands
return avg
}
avg(1, 2, 3);
Note: I have looked everywhere in stack exchange for the answer, but the questions all are different from mine because mine is involving two Ints, the same type and not different two different types.
I would like it if someone could help me to solve the problem which I have.
Despite the error message it seems that you cannot forward the sequence (...) operator. A single call of sumOf(numbers) within the agv() function gives an error cannot invoke sumOf with an argument of type ((Int))
The error is telling you what to do. If you refer to https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html
/ Division.
A binary arithmetic operator that divides the number to its left by the number to its right.
Class of operands: integer, real
Class of result: real
The second argument has to be real. Convert it like so. I don't use xcode, but I think my syntax is correct.
var avg:Float = ( sumOf(numbers) ) / Float( numbers.count )

C++ substring check

I'm given two strings s1, s2 and an integer i. I want to check if the substring of s1 starting at index i of the same length as s2 equals s2. I know this is trivial to implement with a little loop, but I would prefer to use a library method if it exists.
As discussed in the comments, here's an in place solution using string::compare (http://www.cplusplus.com/reference/string/string/compare/)
s1.compare(i,s2.length(),s2)==0
Which would throw an std::out_of_range exception if (i>=s1.length()). Otherwise, it works. To avoid the out_of_range exception, just add something to short circuit the compare:
(i<s1.length()) && (s1.compare(i,s2.length(),s2)==0)
I would do it using std::equal with a check of proper size first. That way it directly works for vectors as well. The check of sizes is necessary to avoid invalid iterators:
if ( i + s2.size() > s1.size() )
{
return false;
}
auto s1beg = s1.cbegin() + i;
auto s1end = s1.cbegin() + i + s2.size();
return std::equal(s1beg, s1end, s2.cbegin());
If you want to support any ranges, use std::advance to create the start and end iterator:
auto s1beg = s1.cbegin();
std::advance(s1beg, i);
auto s2end = s1beg;
std::advance(s2beg, s2.size());
Here's a demo for both strings and a list. A further design decision may allow different container types as well.
bool is_sub_string=i <= s1.size() && s1.substr(i, s2.size()) == s2;

Are there any advantage for passing closure to a method?

I wonder what is the real use of passing a closure object to a method.
Lets say I have a closure :
def a = {
println it
}
(Consider it is doing some sort of operation rather than just printing)
Now I'm just passing this closure a to a method :
def testMethod(def input,Closure a){
a(input)
}
testMethod "MethodPointer", a //passing closure.
Now the question is why this level of indirection? Why can't testMethod directly process its input? Yes, here I'm making input to be processed in closure a, but why one should do so?. What is the real use of passing closure around?
Thanks in advance.
How would you write the collect method without Closures for parameters? It would be a lot more complex than it is currently. The same goes for Builders, inject, each, with and many more...
It allows you to define something generic, and then make it more specific at a later date. For example, the collect method could be described as "takes a collection or iterable, and for each element DO SOMETHING and add it to a newly created collection". Without Closures to specify this DO SOMETHING at a later date, the value of collect would be minimal.
Slowly, you come to a more functional way of thinking. Rather than writing a specific function to perform a specific task, could I write a function which takes more of a generalist approach applicable to multiple problems, and put the specifics for each individual case in a Closure?
Edit
As an example, consider this procedural method which returns a List of the numbers between min and max which are multiples of mult:
List<Integer> filter( int min, int max, int mult ) {
List<Integer> multiples = new ArrayList<Integer>() ;
for( int i = min ; i < max ; i++ ) {
if( i % mult == 0 ) {
multiples.add( i ) ;
}
}
multiples
}
println filter( 1, 200, 15 )
If we write this in Groovy using Closures (for the filtering), we get:
List<Integer> filter( int min, int max, int mult ) {
(min..max).findAll { it % mult == 0 }
}
println filter( 1, 200, 15 )
(I accept that this example is basically mirroring the functionality of findAll, so probably isn't a great example -- and is also somewhat contrived)
Now consider that we want to filter based on some other criteria (that the integer exists in a database or something)... We could first rewrite our filter method to:
List<Integer> filter( int min, int max, Closure<Boolean> filter ) {
(min..max).findAll filter
}
println filter( 1, 200 ) { it % 15 == 0 }
Then, as you can see, we can pass any closure to this method that returns true if we want to keep the element, or false if we want to drop it.
To do this in an imperative style, you're going to (probably) end up with multiple methods all doing very similar tasks...
The idea is to write modular and structured clear code.
I wrote a blog post about Closure Design Patterns. You can find some examples of patterns using closures.

Boost Spirit rule with custom attribute parsing

I am writing a Boost Spirit grammar to parse text into a vector of these structs:
struct Pair
{
double a;
double b;
};
BOOST_FUSION_ADAPT_STRUCT(
Pair,
(double, a)
(double, a)
)
This grammar has a rule like this:
qi::rule<Iterator, Pair()> pairSequence;
However, the actual grammar of pairSequence is this:
double_ % separator
I want this grammar to produce a Pair with a equal to the double and b equal to some constant. I want to do something like this:
pairSequence = double_[_val = Pair(_1, DEFAULT_B)] % separator;
The above does not compile, of course. I tried adding a constructor to Pair, but I still get compile errors (no matching function for call to 'Pair::Pair(const boost::phoenix::actor >&, double)').
First of all, the signature of pairSequence needs to be:
qi::rule<Iterator, std::vector<Pair>()> pairSequence;
as the list operator exposes a std::vector<Pair> as its attribute.
All functions called from inside a semantic action have to be 'lazy', so you need to utilize phoenix:
namespace phx = boost::phoenix;
pairSequence =
double_[
phx::push_back(_val,
phx::construct<Pair>(_1, phx::val(DEFAULT_B))
)
] % separator
;
Another possibility would be to add a (non-explicit) constructor to Pair:
struct Pair
{
Pair(double a) : a(a), b(DEFAULT_B) {}
double a;
double b;
};
which allows to simplify the grammar:
pairSequence = double_ % separator;
and completely relies on Spirit's built-in attribute propagation rules.
BTW, for any of this to work, you don't need to adapt Pair as a Fusion sequence.

Resources