Appending a running number to the parser output in ANTLR - concatenation

I'm quite new to ANTLR and trying to concatenate a number incremented automatically to my parser output. My grammar looks like below. I defined the variable mtCount to store my increment values. The file gets successfully parsed and cpformat includes everything intended but the value of mtCount is not concatenated to the output. Any ideas, what I am doing wrong?
grammar CP3;
#parser::members {
public int mtCount = 0;
}
file returns [String cpformat]
: 'type' t=type {$cpformat += $t.text;}
'variable' v=variable {$cpformat += $v.text;}
'rule' rs=rulestr {$cpformat += $rs.text;}
;
type returns [String cpformat]
: (dn=domname {$cpformat += $dn.text;}
'{' vv1=varval {$cpformat += $vv1.text;}
(',' vv2=varval {$cpformat += $vv2.text;} )* '};')*
;
variable returns [String cpformat]
: (dn=domname {$cpformat += $dn.text;}
vn=varname {$cpformat += $vn.text;} ';')*
;
rulestr returns [String cpformat]
: ('(' '('
mt1=minterm {$cpformat += $mt1.text + " mtCount: " + mtCount++;}
('||' mt2=minterm {$cpformat += $mt2.text + " mtCount: " + mtCount++;})*
')' ')' ';')*
;
minterm returns [String cpformat]
: '(' vn1=varname {$cpformat += $vn1.text;}
'==' vv1=varval {$cpformat += $vv1.text;}
('&&' vn2=varname {$cpformat += $vn2.text;}
'==' vv2=varval {$cpformat += $vv2.text;} )* ')'
;
domname returns [String cpformat]
: str1=STRING {$cpformat = $str1.text;}
;
varname returns [String cpformat]
: str2=STRING {$cpformat = $str2.text;}
;
varval returns [String cpformat]
: str3=STRING {$cpformat = $str3.text;}
;
STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'_')+ ;
WS : ( ' ' | '\t' | '\r' | '\n' )+ {skip();} ;
Neither mtCount, nor its value can be seen in the output. The whole output is a bit lengthy, but I send a part of it below, it should appear in this part of the output for example:
dom97var97;dom98var98;dom99var99;dom100var100;dom101var101;
((
Here, I would expect it.
(var1==S64&&var3==M9&&var81==BVM5&&var94==EU93&&var95==CRIT1870CC&&var96==CRIT095CV&&var99==JC5&&var100==CRIT784&&var101==F8Q)
||(var1==S64&&var3==M9&&var81==BVM5&&var94==EU93&&var95==CRIT1870CC&&var96==CRIT095CV&&var99==JC5&&var100==CRIT786&&var101==F8Q)
||(var1==S64&&var3==MA&&var81==BVM5&&var94==EU96&&var95==CRIT1870CC&&var96==CRIT065CV&&var99==JB1&&var100==CRIT620&&var101==F8Q)
||(var1==S64&&var3==MA&&var81==BVM5&&var94==EU96&&var95==CRIT1870CC&&var96==CRIT065CV&&var99==JB1&&var100==CRIT624&&var101==F8Q)
|| ...

Related

BASH: check if an array element is empty, But when there is a element '0'

I'm learning arrays in bash, and I'm learning to check if an element is empty when there is a element contain integer '0'
For example,
declare -A Rank
Rank[0]=0
if [ ${#Rank[0]} -eq 0 ]; then
echo "Empty"
else
echo "Not empty"
fi
The result is "Not empty" for this case, so I thought this is how we check an element of associative array is empty. However, when I'm building my program:
declare -A Rank
declare -A arrayPoint
num=4
for (( i=0; i<num; i++)); do
j=0
while [ $j -ne $num ]; do
if [ ${#Rank[j]} -eq 0 ]; then # only need to look at this part
Rank[$j]=$i
break;
elif [ "${ArrayPoint[$i]}" -g ]; then
k=$(($num-1))
while [ $k -ne 0 ]; do
Rank[$k]=${Rank[$(($k-1))]}
((--k))
done
Rank[$j]=$i
break;
fi
((j++))
done
done
Rank is an empty array, so when i=0, it should store the integer 0 in this line: Rank[$j]=$i, which means on the next iteration when j=1, it should skip the first if condition and head to the elif, but when I debug, it does not skip the first if. Here are the debug codes.
+ declare -A arrayPoint
+ declare -A Rank
+ num=4
+ (( i=0 ))
+ (( i<num ))
+ j=0
+ '[' 0 -ne 4 ']'
+ '[' 0 -eq 0 ']'
+ Rank[$j]=0
+ break
+ (( i++ ))
+ (( i<num ))
+ j=0
+ '[' 0 -ne 4 ']'
+ '[' 0 -eq 0 ']'
+ Rank[$j]=1
+ break
+ (( i++ ))
+ (( i<num ))
+ j=0
+ '[' 0 -ne 4 ']'
+ '[' 0 -eq 0 ']'
+ Rank[$j]=2
+ break
+ (( i++ ))
+ (( i<num ))
+ j=0
+ '[' 0 -ne 4 ']'
+ '[' 0 -eq 0 ']'
+ Rank[$j]=3
+ break
+ (( i++ ))
+ (( i<num ))
I think the problem should be in if [ ${#Rank[j]} -eq 0 ]; since it's has never go down further than the first if statement. Please let me know if I missed something. Thank you.

Bison C to Matlab

I need to translate a for loop from C to Matlab.
I have the following grammar:
stmt : stmt_while
| stmt_for
|...
;
stmt_for : FOR '(' for_exp ')' stmt { }
;
for_exp : exp ';' exp ';' exp { }
| exp ';' exp ';' { }
| exp ';' ';' exp { }
| exp ';' ';' { }
| ';' exp ';' exp { }
| ';' exp ';' { }
| ';' ';' exp { }
| ';' ';' { }
;
The for loops in Matlab have a completely different syntax so I do not know how to implement the translation.
The simplest solution seems to me to translate it in a while:
exp1;
for(exp1; exp2; exp3){ while(exp2){
for_body... ==> for_body...
} exp3;
}
You have other solutions?
Thank you all.

How can i split string per line on file

I have a file that contain string like this:
- ' *[0-9]-? [^a-c]#[*-^a-c]' '' < temp-test/758.inp.325.1
- ' *[0-9]-? [^a-c]#[*-^a-c]' '' < temp-test/759.inp.325.3
- ' *[0-9]#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1133.inp.487.1`enter code here`
- ' *[0-9]#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1134.inp.487.2
- '"##' 'm' < input/ruin.1890
I want to split this string per line to be 2 part , and i hope the result like this:
- line 1: array[0]=' *[0-9]-? [^a-c]#[*-^a-c]'; array [1]='' < temp-test/758.inp.325.1
- line 2: array[0]=' *[0-9]-? [^a-c]#[*-^a-c]'; array [1]='' < temp-test/759.inp.325.3
- line 3: array[0]=' *[0-9]#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='NEW' < temp-test/1133.inp.487.1
- line 4: array[0]=' *[0-9]#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='NEW' < temp-test/1134.inp.487.2
- line 5: array[0]='"##'; array[1]='m' < input/ruin.1890
and the code i've try is like this:
#!/usr/bin/perl
# location of universe file
$tc = "/root/Desktop/SIEMENS/replace/testplans.alt/universe";
# open file universe;
open( F, "<$tc" );
#test_case = <F>;
while ( $i < 5 ) {
$test_case[$i] =~ s/ //;
#isi = split( / /, $test_case[$i] );
if ( $#isi == 2 ) {
print "Input1:" . $isi[0] . "\n";
print "Input2:" . $isi[1] . "\n";
print "Input3:" . $isi[2] . "\n";
}
$i++;
}
I am confused because i can't slit that string with " " (space), because each of line have different order space and i can't get to be 2 part.
Thank you.
use strict;
use warnings;
# this stuff just gives me your data for testing
my $data =<<EOF;
' [0-9]-? [^a-c]\#[-^a-c]' '' < temp-test/758.inp.325.1
' [0-9]-? [^a-c]\#[-^a-c]' '' < temp-test/759.inp.325.3
' *[0-9]\#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1133.inp.487.1
' *[0-9]\#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1134.inp.487.2
'"\#\#' 'm' < input/ruin.1890
EOF
for my $line (split(/\n/,$data))
{
# this re splits your strings according
# to what I perceive to be your requirements
if ($line =~ /^(.*)('.*?' <.*)$/)
{
print("array[0]=$1; array[1]=$2;\n")
}
}
1;
Output:
array[0]=' [0-9]-? [^a-c]#[-^a-c]' ; array[1]='' < temp-test/758.inp.325.1;
array[0]=' [0-9]-? [^a-c]#[-^a-c]' ; array[1]='' < temp-test/759.inp.325.3;
array[0]=' *[0-9]#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='NEW' < temp-test/1133.inp.487.1
array[0]=' *[0-9]#[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='NEW' < temp-test/1134.inp.487.2;
array[0]='"##' ; array[1]='m' < input/ruin.1890;
Applied to your code it might look something like this:
while ($i<5)
{
$test_case[$i] =~ /^(.*)('.*?' <.*)$/;
#isi = ($1,$2);
print "Input1:".$isi[0]."\n";
print "Input2:".$isi[1]."\n";
$i++;
}

Bison's analysis Pascal grammar in C

I am writing program to analyse Pascal grammar. I want to check correctness of input Pascal file and show where errors are.
I have a problem with finding more than one error, after finding an error parser ends.
Also parser doesnt't show in which line error is only display " Syntax error at or before [declaration], line" but i want to this line where is an error.
I used : http://ccia.ei.uvigo.es/docencia/PL/doc/bison/pascal/
pascal.l
%{
/*
* pascal.l
*
* lex input file for pascal scanner
*
* extensions: to ways to spell "external" and "->" ok for "^".
*/
#include <stdio.h>
#include "pascal.tab.h"
int line_no = 1;
%}
A [aA]
B [bB]
C [cC]
D [dD]
E [eE]
F [fF]
G [gG]
H [hH]
I [iI]
J [jJ]
K [kK]
L [lL]
M [mM]
N [nN]
O [oO]
P [pP]
Q [qQ]
R [rR]
S [sS]
T [tT]
U [uU]
V [vV]
W [wW]
X [xX]
Y [yY]
Z [zZ]
NQUOTE [^']
%%
{A}{N}{D} return(AND);
{A}{R}{R}{A}{Y} return(ARRAY);
{C}{A}{S}{E} return(CASE);
{C}{O}{N}{S}{T} return(CONST);
{D}{I}{V} return(DIV);
{D}{O} return(DO);
{D}{O}{W}{N}{T}{O} return(DOWNTO);
{E}{L}{S}{E} return(ELSE);
{E}{N}{D} return(END);
{E}{X}{T}{E}{R}{N} |
{E}{X}{T}{E}{R}{N}{A}{L} return(EXTERNAL);
{F}{O}{R} return(FOR);
{F}{O}{R}{W}{A}{R}{D} return(FORWARD);
{F}{U}{N}{C}{T}{I}{O}{N} return(FUNCTION);
{G}{O}{T}{O} return(GOTO);
{I}{F} return(IF);
{I}{N} return(IN);
{L}{A}{B}{E}{L} return(LABEL);
{M}{O}{D} return(MOD);
{N}{I}{L} return(NIL);
{N}{O}{T} return(NOT);
{O}{F} return(OF);
{O}{R} return(OR);
{O}{T}{H}{E}{R}{W}{I}{S}{E} return(OTHERWISE);
{P}{A}{C}{K}{E}{D} return(PACKED);
{B}{E}{G}{I}{N} return(PBEGIN);
{F}{I}{L}{E} return(PFILE);
{P}{R}{O}{C}{E}{D}{U}{R}{E} return(PROCEDURE);
{P}{R}{O}{G}{R}{A}{M} return(PROGRAM);
{R}{E}{C}{O}{R}{D} return(RECORD);
{R}{E}{P}{E}{A}{T} return(REPEAT);
{S}{E}{T} return(SET);
{T}{H}{E}{N} return(THEN);
{T}{O} return(TO);
{T}{Y}{P}{E} return(TYPE);
{U}{N}{T}{I}{L} return(UNTIL);
{V}{A}{R} return(VAR);
{W}{H}{I}{L}{E} return(WHILE);
{W}{I}{T}{H} return(WITH);
[a-zA-Z]([a-zA-Z0-9\-])* return(IDENTIFIER);
":=" return(ASSIGNMENT);
'({NQUOTE}|'')+' return(CHARACTER_STRING);
":" return(COLON);
"," return(COMMA);
[0-9]+ return(DIGSEQ);
"." return(DOT);
".." return(DOTDOT);
"=" return(EQUAL);
">=" return(GE);
">" return(GT);
"[" return(LBRAC);
"<=" return(LE);
"(" return(LPAREN);
"<" return(LT);
"-" return(MINUS);
"<>" return(NOTEQUAL);
"+" return(PLUS);
"]" return(RBRAC);
[0-9]+"."[0-9]+ return(REALNUMBER);
")" return(RPAREN);
";" return(SEMICOLON);
"/" return(SLASH);
"*" return(STAR);
"**" return(STARSTAR);
"->" |
"^" return(UPARROW);
"(*" |
"{" { register int c;
while ((c = input()))
{
if (c == '}')
break;
else if (c == '*')
{
if ((c = input()) == ')')
break;
else
unput (c);
}
else if (c == '\n')
line_no++;
else if (c == 0)
commenteof();
}
}
[\t\f " "] ;
\n line_no++;
. { fprintf (stderr,
"'%c' (0%o): illegal character at line %d\n",
yytext[0], yytext[0], line_no);
}
%%
commenteof()
{
fprintf (stderr, "Unexpected EOF inside comment at line %d\n", line_no);
exit (1);
}
yywrap ()
{
return (1);
}
pascal.y
%{
/*
* pascal.y
*
* Pascal grammar in Yacc format, based originally on BNF given
* in "Standard Pascal -- User Reference Manual", by Doug Cooper.
* This in turn is the BNF given by the ANSI and ISO Pascal standards,
* and so, is PUBLIC DOMAIN. The grammar is for ISO Level 0 Pascal.
* The grammar has been massaged somewhat to make it LALR, and added
* the following extensions.
*
* constant expressions
* otherwise statement in a case
* productions to correctly match else's with if's
* beginnings of a separate compilation facility
*/
#include<stdio.h>
%}
%token AND ARRAY ASSIGNMENT CASE CHARACTER_STRING
%token COLON COMMA CONST DIGSEQ DIV DO DOT DOTDOT
%token DOWNTO ELSE END EQUAL EXTERNAL FOR FORWARD
%token FUNCTION GE GOTO GT IDENTIFIER IF IN LABEL LBRAC
%token LE LPAREN LT MINUS MOD NIL NOT NOTEQUAL OF OR
%token OTHERWISE PACKED PBEGIN PFILE PLUS PROCEDURE
%token PROGRAM RBRAC REALNUMBER RECORD REPEAT RPAREN
%token SEMICOLON SET SLASH STAR STARSTAR THEN
%token TO TYPE UNTIL UPARROW VAR WHILE WITH
%%
file : program
| module
;
program : program_heading semicolon block DOT
;
program_heading : PROGRAM identifier
| PROGRAM identifier LPAREN identifier_list RPAREN
;
identifier_list : identifier_list COMMA identifier
| identifier
;
block : label_declaration_part
constant_definition_part
type_definition_part
variable_declaration_part
procedure_and_function_declaration_part
statement_part
;
module : constant_definition_part
type_definition_part
variable_declaration_part
procedure_and_function_declaration_part
;
label_declaration_part : LABEL label_list semicolon
|
;
label_list : label_list comma label
| label
;
label : DIGSEQ
;
constant_definition_part : CONST constant_list
|
;
constant_list : constant_list constant_definition
| constant_definition
;
constant_definition : identifier EQUAL cexpression semicolon
;
/*constant : cexpression ; /* good stuff! */
cexpression : csimple_expression
| csimple_expression relop csimple_expression
;
csimple_expression : cterm
| csimple_expression addop cterm
;
cterm : cfactor
| cterm mulop cfactor
;
cfactor : sign cfactor
| cexponentiation
;
cexponentiation : cprimary
| cprimary STARSTAR cexponentiation
;
cprimary : identifier
| LPAREN cexpression RPAREN
| unsigned_constant
| NOT cprimary
;
constant : non_string
| sign non_string
| CHARACTER_STRING
;
sign : PLUS
| MINUS
;
non_string : DIGSEQ
| identifier
| REALNUMBER
;
type_definition_part : TYPE type_definition_list
|
;
type_definition_list : type_definition_list type_definition
| type_definition
;
type_definition : identifier EQUAL type_denoter semicolon
;
type_denoter : identifier
| new_type
;
new_type : new_ordinal_type
| new_structured_type
| new_pointer_type
;
new_ordinal_type : enumerated_type
| subrange_type
;
enumerated_type : LPAREN identifier_list RPAREN
;
subrange_type : constant DOTDOT constant
;
new_structured_type : structured_type
| PACKED structured_type
;
structured_type : array_type
| record_type
| set_type
| file_type
;
array_type : ARRAY LBRAC index_list RBRAC OF component_type
;
index_list : index_list comma index_type
| index_type
;
index_type : ordinal_type ;
ordinal_type : new_ordinal_type
| identifier
;
component_type : type_denoter ;
record_type : RECORD record_section_list END
| RECORD record_section_list semicolon variant_part END
| RECORD variant_part END
;
record_section_list : record_section_list semicolon record_section
| record_section
;
record_section : identifier_list COLON type_denoter
;
variant_part : CASE variant_selector OF variant_list semicolon
| CASE variant_selector OF variant_list
|
;
variant_selector : tag_field COLON tag_type
| tag_type
;
variant_list : variant_list semicolon variant
| variant
;
variant : case_constant_list COLON LPAREN record_section_list RPAREN
| case_constant_list COLON LPAREN record_section_list semicolon
variant_part RPAREN
| case_constant_list COLON LPAREN variant_part RPAREN
;
case_constant_list : case_constant_list comma case_constant
| case_constant
;
case_constant : constant
| constant DOTDOT constant
;
tag_field : identifier ;
tag_type : identifier ;
set_type : SET OF base_type
;
base_type : ordinal_type ;
file_type : PFILE OF component_type
;
new_pointer_type : UPARROW domain_type
;
domain_type : identifier ;
variable_declaration_part : VAR variable_declaration_list semicolon
|
;
variable_declaration_list :
variable_declaration_list semicolon variable_declaration
| variable_declaration
;
variable_declaration : identifier_list COLON type_denoter
;
procedure_and_function_declaration_part :
proc_or_func_declaration_list semicolon
|
;
proc_or_func_declaration_list :
proc_or_func_declaration_list semicolon proc_or_func_declaration
| proc_or_func_declaration
;
proc_or_func_declaration : procedure_declaration
| function_declaration
;
procedure_declaration : procedure_heading semicolon directive
| procedure_heading semicolon procedure_block
;
procedure_heading : procedure_identification
| procedure_identification formal_parameter_list
;
directive : FORWARD
| EXTERNAL
;
formal_parameter_list : LPAREN formal_parameter_section_list RPAREN ;
formal_parameter_section_list :
formal_parameter_section_list semicolon formal_parameter_section
| formal_parameter_section
;
formal_parameter_section : value_parameter_specification
| variable_parameter_specification
| procedural_parameter_specification
| functional_parameter_specification
;
value_parameter_specification : identifier_list COLON identifier
;
variable_parameter_specification : VAR identifier_list COLON identifier
;
procedural_parameter_specification : procedure_heading ;
functional_parameter_specification : function_heading ;
procedure_identification : PROCEDURE identifier ;
procedure_block : block ;
function_declaration : function_heading semicolon directive
| function_identification semicolon function_block
| function_heading semicolon function_block
;
function_heading : FUNCTION identifier COLON result_type
| FUNCTION identifier formal_parameter_list COLON result_type
;
result_type : identifier ;
function_identification : FUNCTION identifier ;
function_block : block ;
statement_part : compound_statement ;
compound_statement : PBEGIN statement_sequence END ;
statement_sequence : statement_sequence semicolon statement
| statement
;
statement : open_statement
| closed_statement
;
open_statement : label COLON non_labeled_open_statement
| non_labeled_open_statement
;
closed_statement : label COLON non_labeled_closed_statement
| non_labeled_closed_statement
;
non_labeled_closed_statement : assignment_statement
| procedure_statement
| goto_statement
| compound_statement
| case_statement
| repeat_statement
| closed_with_statement
| closed_if_statement
| closed_while_statement
| closed_for_statement
|
;
non_labeled_open_statement : open_with_statement
| open_if_statement
| open_while_statement
| open_for_statement
;
repeat_statement : REPEAT statement_sequence UNTIL boolean_expression
;
open_while_statement : WHILE boolean_expression DO open_statement
;
closed_while_statement : WHILE boolean_expression DO closed_statement
;
open_for_statement : FOR control_variable ASSIGNMENT initial_value direction
final_value DO open_statement
;
closed_for_statement : FOR control_variable ASSIGNMENT initial_value direction
final_value DO closed_statement
;
open_with_statement : WITH record_variable_list DO open_statement
;
closed_with_statement : WITH record_variable_list DO closed_statement
;
open_if_statement : IF boolean_expression THEN statement
| IF boolean_expression THEN closed_statement ELSE open_statement
;
closed_if_statement : IF boolean_expression THEN closed_statement
ELSE closed_statement
;
assignment_statement : variable_access ASSIGNMENT expression
;
variable_access : identifier
| indexed_variable
| field_designator
| variable_access UPARROW
;
indexed_variable : variable_access LBRAC index_expression_list RBRAC
;
index_expression_list : index_expression_list comma index_expression
| index_expression
;
index_expression : expression ;
field_designator : variable_access DOT identifier
;
procedure_statement : identifier params
| identifier
;
params : LPAREN actual_parameter_list RPAREN ;
actual_parameter_list : actual_parameter_list comma actual_parameter
| actual_parameter
;
/*
* this forces you to check all this to be sure that only write and
* writeln use the 2nd and 3rd forms, you really can't do it easily in
* the grammar, especially since write and writeln aren't reserved
*/
actual_parameter : expression
| expression COLON expression
| expression COLON expression COLON expression
;
goto_statement : GOTO label
;
case_statement : CASE case_index OF case_list_element_list END
| CASE case_index OF case_list_element_list semicolon END
| CASE case_index OF case_list_element_list semicolon
otherwisepart statement END
| CASE case_index OF case_list_element_list semicolon
otherwisepart statement semicolon END
;
case_index : expression ;
case_list_element_list : case_list_element_list semicolon case_list_element
| case_list_element
;
case_list_element : case_constant_list COLON statement
;
otherwisepart : OTHERWISE
| OTHERWISE COLON
;
control_variable : identifier ;
initial_value : expression ;
direction : TO
| DOWNTO
;
final_value : expression ;
record_variable_list : record_variable_list comma variable_access
| variable_access
;
boolean_expression : expression ;
expression : simple_expression
| simple_expression relop simple_expression
;
simple_expression : term
| simple_expression addop term
;
term : factor
| term mulop factor
;
factor : sign factor
| exponentiation
;
exponentiation : primary
| primary STARSTAR exponentiation
;
primary : variable_access
| unsigned_constant
| function_designator
| set_constructor
| LPAREN expression RPAREN
| NOT primary
;
unsigned_constant : unsigned_number
| CHARACTER_STRING
| NIL
;
unsigned_number : unsigned_integer | unsigned_real ;
unsigned_integer : DIGSEQ
;
unsigned_real : REALNUMBER
;
/* functions with no params will be handled by plain identifier */
function_designator : identifier params
;
set_constructor : LBRAC member_designator_list RBRAC
| LBRAC RBRAC
;
member_designator_list : member_designator_list comma member_designator
| member_designator
;
member_designator : member_designator DOTDOT expression
| expression
;
addop: PLUS
| MINUS
| OR
;
mulop : STAR
| SLASH
| DIV
| MOD
| AND
;
relop : EQUAL
| NOTEQUAL
| LT
| GT
| LE
| GE
| IN
;
identifier : IDENTIFIER
;
semicolon : SEMICOLON
;
comma : COMMA
;
%%
extern int line_no;
extern char *yytext;
int yyerror(s)
char *s;
{
fprintf(stderr, "%s: at or before '%s', line %d\n",
s, yytext, line_no);
}
main (void) {
extern int init();
extern FILE *yyin;
extern int yylex();
extern int yylineno;
extern char *yytext;
yyin = fopen("D:\\helloword.pascal", "r");
if(yyparse()){
printf("error");
}
else {
printf("good");
}
fclose(yyin);
getchar();
}
Pascal input file
program Hello;
begin someErrorText1
writeln ('Hello, world.') someErrorText2
writeln ('Hello, world2.')
end.
output
Console
And you see error is in line 2 but parser show 3, and doesn't show second error.

Perl - Splitting a string

I'm doing an Array that contents each word of a phrase. When I try to split it and print the length then the console gives me an enormous number such as 111039391231319239188238139123919232913123... (more lines)
why?
Here's my code:
$mynames = $texto3;
print $mynames. "\n";
#nameList = split(' ', $texto3);
#print #nameList.length();
for ($to = 0; $to<#nameList.length; $to++){
if($to<#nameList.length) {
#nameList[$to] = #nameList[$to] . "_" . #nameList[$to++];
}
print $to;
#print #nameList[$to] . "\n";
}
$string_level2 = join(' ', #nameList);
#print $string_level2;
To get the length of an array use scalar #nameList instead of #nameList.length.
A typical for-loop uses the less-than operator when counting up, e.g.:
for ( $to = 0; $to < scalar(#nameList); $to++ ) ...
You should never use a post-increment unless you understand the side effects. I believe the following line:
#nameList[$to] = #nameList[$to] . "_" . #nameList[$to++];
... should be written as ...
$nameList[$to] = $nameList[$to] . "_" . $nameList[$to + 1];
Finally the comparison you use should account for the boundary condition (because you refer to $to + 1 inside the loop):
if( $to < (scalar(#nameList) - 1) ) {
$nameList[ $to ] = $nameList[ $to ] . "_" . $nameList[ $to + 1 ];
}

Resources