Shift Reduce Conflict (CUP) - jflex

I am trying define my grammar in my cup file and I'm getting the following errors.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) INT
under symbol INT
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) CHAR
under symbol CHAR
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) BOOL
under symbol BOOL
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #4
between fielddecls ::= (*)
and type ::= (*) FLOAT
under symbol FLOAT
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) INT
under symbol INT
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) CHAR
under symbol CHAR
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) BOOL
under symbol BOOL
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #8
between fielddecls ::= (*)
and type ::= (*) FLOAT
under symbol FLOAT
Resolved in favor of shifting.
The following is the grammar that is defined in my cup file.
program ::= CLASS ID:i CURLBRACKETBEGIN memberdecls:m CURLBRACKETEND
{: RESULT = new Program(i,m); :};
memberdecls ::= fielddecls:fds methoddecls:m
{: RESULT = new Memberdecls(fds,m); :};
fielddecls ::= fielddecl:f fielddecls:fds
{: RESULT = new Fielddecls(f,fds); :}
|
{: RESULT = new Fielddecls(); :};
methoddecls ::= methoddecl:md methoddecls:mds
{: RESULT = new Methoddecls(md, mds); :}
|
{: RESULT = new Methoddecls(); :};
methoddecl ::= type:t ID:i PARENTHESISBEGIN argdecls:a PARENTHESISEND CURLBRACKETBEGIN fielddecls:f stmts:st CURLBRACKETEND optionalsemi:s
{: RESULT = new Methoddecl(t,i,a,f,st,s); :}
|
VOID ID:i PARENTHESISBEGIN argdecls:a PARENTHESISEND CURLBRACKETBEGIN fielddecls:f stmts:st CURLBRACKETEND optionalsemi:s
{: RESULT = new Methoddecl(i,a,f,st,s); :}
;
type ::= INT
{: RESULT = new Type("int"); :}
|
CHAR
{: RESULT = new Type("char"); :}
|
BOOL
{: RESULT = new Type("bool"); :}
|
FLOAT
{: RESULT = new Type("float"); :};
optionalsemi ::= SEMI
{: RESULT = new Optionalsemi(";"); :}
|
{: RESULT = new Optionalsemi(); :};
fielddecl ::= FINAL type:t ID:i optionalexpr:oe SEMI
{: RESULT = new Fielddecl("final", t, i, oe); :}
|
type:t ID:i optionalexpr:oe SEMI
{: RESULT = new Fielddecl(t,i,oe); :}
|
type:t ID:i SQUAREBRACKETBEGIN INTLIT:intl SQUAREBRACKETEND
I believe this has something to do with how Fielddecls is referring to the Type nonterminal. However, I'm not really sure how to eliminate it. If possible, can someone please offer some guidance to eliminate these shift reduce errors?

Related

Problem compiling emacs 23.4.1 on Ubuntu 20.0.4.2 LTS

I followed the instructions in INSTALL for dependency installation and configuration. Then, after make, I get these errors:
In file included from ./config.h:1074,
from dispnew.c:21:
dispnew.c: In function ‘update_frame_1’:
./s/gnu-linux.h:164:10: error: ‘FILE’ {aka ‘struct _IO_FILE’} has no member named ‘_pptr’
164 | ((FILE)->_pptr - (FILE)->_pbase)
| ^~
dispnew.c:89:30: note: in expansion of macro ‘GNU_LIBRARY_PENDING_OUTPUT_COUNT’
89 | #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dispnew.c:5384:16: note: in expansion of macro ‘PENDING_OUTPUT_COUNT’
5384 | int outq = PENDING_OUTPUT_COUNT (display_output);
| ^~~~~~~~~~~~~~~~~~~~
./s/gnu-linux.h:164:26: error: ‘FILE’ {aka ‘struct _IO_FILE’} has no member named ‘_pbase’
164 | ((FILE)->_pptr - (FILE)->_pbase)
| ^~
dispnew.c:89:30: note: in expansion of macro ‘GNU_LIBRARY_PENDING_OUTPUT_COUNT’
89 | #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dispnew.c:5384:16: note: in expansion of macro ‘PENDING_OUTPUT_COUNT’
5384 | int outq = PENDING_OUTPUT_COUNT (display_output);
How do I get past these errors?

how to to create a mulitline macro in julia?

macro Estruct(name,arg,type,max=100,min=0,descritpion="")
:(struct $(esc(name))
$((esc(arg)))
$(esc(name))($(esc(arg)))=new(
check($(esc(type)),$(esc(arg)),$(esc(name)),$(esc(max)),$(esc(min)),$(esc(descritpion))))
end)
end
how can I imeplent this macro like this:
#Estruct begin
B
arg1
Float64
200.0
5.0
"this"
end
I don't know how to make a multiline macro. I thought I just have to add begin and end, but I'm always getting: MethodError: no method matching var"#Estruct"(::LineNumberNode, ::Module, ::Expr)
There's no such thing as a multiline macro, that's just a macro that takes a block as an argument. You can see how the macro gets invoked by writing a dummy version of it that just returns its arguments:
macro Estruct(args...); args; end
Now you can invoke that the way you want to call it and it will return its arguments as a tuple:
julia> args = #Estruct begin
B
arg1
Float64
200.0
5.0
"this"
end
(quote
#= REPL[12]:2 =#
B
#= REPL[12]:3 =#
arg1
#= REPL[12]:4 =#
Float64
#= REPL[12]:5 =#
200.0
#= REPL[12]:6 =#
5.0
#= REPL[12]:7 =#
"this"
end,)
julia> typeof(args)
Tuple{Expr}
julia> dump(args[1])
Expr
head: Symbol block
args: Array{Any}((12,))
1: LineNumberNode
line: Int64 2
file: Symbol REPL[12]
2: Symbol B
3: LineNumberNode
line: Int64 3
file: Symbol REPL[12]
4: Symbol arg1
5: LineNumberNode
line: Int64 4
file: Symbol REPL[12]
...
8: Float64 200.0
9: LineNumberNode
line: Int64 6
file: Symbol REPL[12]
10: Float64 5.0
11: LineNumberNode
line: Int64 7
file: Symbol REPL[12]
12: String "this"
julia> args[1].args
12-element Vector{Any}:
:(#= REPL[12]:2 =#)
:B
:(#= REPL[12]:3 =#)
:arg1
:(#= REPL[12]:4 =#)
:Float64
:(#= REPL[12]:5 =#)
200.0
:(#= REPL[12]:6 =#)
5.0
:(#= REPL[12]:7 =#)
"this"
This tells you that the macro gets called with a single argument which is an Expr with head type :block, i.e. the one argument is a quoted block. To implement your "multiline macro" you need to implement the macro body to accept a quoted block expression and process it, presumably by looking at its .args field which is where all the expressions you're interested in are. You'll probably want to ignore all the LineNumberNode objects in there and just process the other items in the block.

Type error resolving infix expression when compiling simple decoder

This is the code in vhdl:
library IEEE;
use IEEE.std_logic_1164.all;
entity DECODER_TWO is
port
(
SW : in std_logic_vector(2 downto 1);
LD : out std_logic_vector(4 downto 1)
);
end DECODER_TWO;
architecture MY_FIRST_DECODER of DECODER_TWO is
begin
with SW select
LD <= "1110" when "00",
LD <= "1101" when "01",
LD <= "1011" when "10",
LD <= "0111" when "11",
LD <= "1111" when others;
end MY_FIRST_DECODER;
When I try to compile this very simple 2 to 4-bit decoder I get the error message, no matter what I do
# ** Error: C:/skole/in3160/oblig2/DecoderArchitect.vhd(15): Type error resolving infix expression "<=" as type ieee.std_logic_1164.STD_LOGIC_VECTOR.
# ** Error: C:/skole/in3160/oblig2/DecoderArchitect.vhd(16): Type error resolving infix expression "<=" as type ieee.std_logic_1164.STD_LOGIC_VECTOR.
# ** Error: C:/skole/in3160/oblig2/DecoderArchitect.vhd(17): Type error resolving infix expression "<=" as type ieee.std_logic_1164.STD_LOGIC_VECTOR.
# ** Error: C:/skole/in3160/oblig2/DecoderArchitect.vhd(18): Type error resolving infix expression "<=" as type ieee.std_logic_1164.STD_LOGIC_VECTOR.
# ** Error: C:/skole/in3160/oblig2/DecoderArchitect.vhd(19): VHDL Compiler exiting
I can't see or understand what the problem is, since I don't get an error for '<=' in the first line, only line 2, 3, 4, and 5.

Xtext 2.8+ formatter, formatting HiddenRegion with comment

I am using Xtext 2.9 formatter and I am trying to format hiddenRegion which contains comment. Here is part of my document region i am trying to format:
Columns: 1:offset 2:length 3:kind 4: text 5:grammarElement
Kind: H=IHiddenRegion S=ISemanticRegion B/E=IEObjectRegion
35 0 H
35 15 S ""xxx::a::b"" Refblock:namespace=Namespace
50 0 H
50 1 S "}" Refblock:RCBRACKET
E Refblock PackageHead:Block=Refblock path:PackageHead/Block=Package'xxx_constants'/head=Model/packages[0]
51 0 H
51 1 S ":" PackageHead:COLON
E PackageHead Package:head=PackageHead path:Package'xxx_constants'/head=Model/packages[0]
52 >>>H "\n " Whitespace:TerminalRule'WS'
"# asd" Comment:TerminalRule'SL_COMMENT'
15 "\n " Whitespace:TerminalRule'WS'<<<
B Error'ASSD' Package:expressions+=Expression path:Package'xxx_constants'/expressions[0]=Model/packages[0]
67 5 S "error" Error:'error'
72 1 H " " Whitespace:TerminalRule'WS'
and corresponding part of the grammar
Model:
{Model}
(packages+=Package)*;
Expression:
Error | Warning | Enum | Text;
Package:
{Package}
'package' name=Name head=PackageHead
(BEGIN
(imports+=Import)*
(expressions+=Expression)*
END)?;
Error:
{Error}
('error') name=ENAME parameter=Parameter COLON
(BEGIN
(expressions+=Estatement)+
END)?;
PackageHead:
Block=Refblock COLON;
Problem is that when i try prepend some characters before error keyword
for example
error.regionFor.keyword('error').prepend[setSpace("\n ")]
This indentation is prepended before the comment and not behind it. This results into improper formatting in case of single line comment before the 'error' keyword.
To provide more clarity, here is example code from my grammar and description of desired behavior:
package xxx_constants {namespace="xxx::a::b"}:
# asd
error ASSD {0}:
Hello {0,world}
This is expected result: (one space to the left)
package xxx_constants {namespace="xxx::a::b"}:
# asd
error ASSD {0}:
Hello {0,world}
and this is the actual result with prepend method
package xxx_constants {namespace="xxx::a::b"}:
# asd
error ASSD {0}:
Hello {0,world}
As the document structure says, the HiddenRegion is in this case is the statement:
# asd
error
How can i prepend my characters directly before the keyword 'error' and not before the comment? Thanks.
I assume you're creating an indentation-sensitive language, because you're explicitly calling BEGIN and END.
For indentation-sensitive language my answer is: You'll want to overwrite
org.eclipse.xtext.formatting2.internal.HiddenRegionReplacer.applyHiddenRegionFormatting(List<ITextReplacer>)
The methods append[] and prepend[] you're using are agnostic to comments and at a later time applyHiddenRegionFormatting() is called to decide how that formatting is weaved between comments.
To make Xtext use your own subclass of HiddenRegionReplacer, overwrite
org.eclipse.xtext.formatting2.AbstractFormatter2.createHiddenRegionReplacer(IHiddenRegion, IHiddenRegionFormatting)
For languages that do not do whitespace-sensitive lexing/parsing (that's the default) the answer is to not call setSpace() to create indentation or line-wraps.
Instead, do
pkg.interior[indent]
pkg.regionFor.keyword(":").append[newLine]
pkg.append[newLine]

ANTLR: loop did not match anything at input

sorry for my english! I have problem, faced here with such a problem, give the decision:
line 1:0 required (...)+ loop did not match anything at input < E O F
>
This my file, calc.g
grammar calc;
options {
language = Java;
}
rule: statement+;
statement
: expr NEWLINE
| ID '=' expr NEWLINE
| NEWLINE
;
NEWLINE : '\r'? '\n'
;
expr
: multExpression ('+' multExpression |'-' multExpression)*
;
multExpression
: a1=atom ('*' a2=atom | '/' a2=atom)*
;
atom
: ID
| INT
| '(' expr ')'
;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
INT : ('1'..'9') ('0'..'9')*
;
this is my main:
ANTLRReaderStream input = new ANTLRReaderStream();
calcLexer lexer = new calcLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
calcParser parser = new calcParser(tokens);
parser.rule();
It looks like your input is empty: the parser immediately encounters the EOF (end-of-file) where it expects at least one statement.
Try something like this:
ANTLRStringStream input = new ANTLRStringStream("2*42\n");
calcLexer lexer = new calcLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
calcParser parser = new calcParser(tokens);
parser.rule();
As 280Z28 mentioned in the comment beneath my answer, you should probably force the parser to consume the entire input by adding EOF at the end of your parser's entry point:
rule: statement+ EOF;
and if you want to allow an empty string to be valid input too, change the + to a *;
rule: statement* EOF;
I didn't test it but think you have to add the default EOF token to your grammar:
rule: statement+ EOF!;
Your parser seems to recognize the whole input but at the end there is an EOF but you did not add the corresponding rule to your grammar.