how do I filter my values using an array? - arrays

I am trying to sift through some free text answers on geographical locations. As one of the steps, I want to check if the answer is any of the 290 municipalities in my country. As 290 entries would make my code cumbersome/hard to read I try saving them in an array, like below:
Data resorTEST;
keep R_res_ort_namn R_res_ort_txt R_kom_lan R_kommun;
set resor1TEST resor2TEST resor3TEST;
R_res_ort_namn=strip(lowcase(R_res_ort_namn));
R_res_ort_txt=strip(lowcase(R_res_ort_txt));
R_kom_lan=strip(lowcase(R_kom_lan));
array kommuner{290} $ ("upplands väsby" "vallentuna" "österåker" "värmdö"
"järfälla" "ekerö" "huddinge" "botkyrka" "salem"
"haninge" "tyresö" "upplands-bro" "nykvarn" "täby"
"danderyd" "sollentuna" "stockholm" "södertälje" "nacka" "sundbyberg"
"solna" "lidingö" "vaxholm" "norrtälje" "sigtuna" "nynäshamn"
"håbo" "älvkarleby" "knivsta" "heby" "tierp" "uppsala"
"enköping" "östhammar" "vingåker" "gnesta" "nyköping" "oxelösund" "flen"
"katrineholm" "eskilstuna" "strängnäs" "trosa" "ödeshög" "ydre"
"kinda" "boxholm" "åtvidaberg" "finspång" "valdemarsvik" "linköping"
"norrköping" "söderköping" "motala" "vadstena" "mjölby" "aneby"
"gnosjö" "mullsjö" "habo" "gislaved" "vaggeryd" "jönköping"
"nässjö" "värnamo" "sävsjö" "vetlanda" "eksjö" "tranås"
"uppvidinge" "lessebo" "tingsryd" "alvesta" "älmhult" "markaryd"
"växjö" "ljungby" "högsby" "torsås" "mörbylånga" "hultsfred"
"mönsterås" "emmaboda" "kalmar" "nybro" "oskarshamn" "västervik"
"vimmerby" "borgholm" "gotland" "olofström" "karlskrona" "ronneby"
"karlshamn" "sölvesborg" "svalöv" "staffanstorp" "burlöv" "vellinge"
"östra göinge" "örkelljunga" "bjuv" "kävlinge" "lomma" "svedala"
"skurup" "sjöbo" "hörby" "höör" "tomelilla" "bromölla"
"osby" "perstorp" "klippan" "åstorp" "båstad" "malmö"
"lund" "landskrona" "helsingborg" "höganäs" "eslöv" "ystad"
"trelleborg" "kristianstad" "simrishamn" "ängelholm" "hässleholm" "hylte"
"halmstad" "laholm" "falkenberg" "varberg" "kungsbacka" "härryda"
"partille" "öckerö" "stenungsund" "tjörn" "orust" "sotenäs"
"munkedal" "tanum" "dals-ed" "färgelanda" "ale" "lerum"
"vårgårda" "bollebygd" "grästorp" "essunga" "karlsborg" "gullspång"
"tranemo" "bengtsfors" "mellerud" "lilla edet" "mark" "svenljunga"
"herrljunga" "vara" "götene" "tibro" "töreboda" "göteborg"
"mölndal" "kungälv" "lysekil" "uddevalla" "strömstad" "vänersborg"
"trollhättan" "alingsås" "borås" "ulricehamn" "åmål" "mariestad"
"lidköping" "skara" "skövde" "hjo" "tidaholm" "falköping"
"kil" "eda" "torsby" "storfors" "hammarö" "munkfors"
"forshaga" "grums" "årjäng" "sunne" "karlstad" "kristinehamn"
"filipstad" "hagfors" "arvika" "säffle" "lekeberg" "laxå"
"hallsberg" "degerfors" "hällefors" "ljusnarsberg" "örebro" "kumla"
"askersund" "karlskoga" "nora" "lindesberg" "skinnskatteberg" "surahammar"
"kungsör" "hallstahammar" "norberg" "västerås" "sala" "fagersta"
"köping" "arboga" "vansbro" "malung-sälen" "gagnef" "leksand"
"rättvik" "orsa" "älvdalen" "smedjebacken" "mora" "falun"
"borlänge" "säter" "hedemora" "avesta" "ludvika" "ockelbo"
"hofors" "ovanåker" "nordanstig" "ljusdal" "gävle" "sandviken"
"söderhamn" "bollnäs" "hudiksvall" "ånge" "timrå" "härnösand"
"sundsvall" "kramfors" "sollefteå" "örnsköldsvik" "ragunda" "bräcke"
"krokom" "strömsund" "åre" "berg" "härjedalen" "östersund"
"nordmaling" "bjurholm" "vindeln" "robertsfors" "norsjö" "malå"
"storuman" "sorsele" "dorotea" "vännäs" "vilhelmina" "åsele"
"umeå" "lycksele" "skellefteå" "arvidsjaur" "arjeplog" "jokkmokk"
"överkalix" "kalix" "övertorneå" "pajala" "gällivare" "älvsbyn"
"luleå" "piteå" "boden" "haparanda" "kiruna");
/*if not missing(R_res_ort_namn) then R_kommun=prxchange("s/^.*-(.* kommun)/$1/",1,R_res_ort_namn);
else if prxmatch("/^.*([a-zA-Z]*? kommun).*$/",R_res_ort_txt) then R_kommun=prxchange("s/^.*?([a-zA-Z]*? kommun).*$/$1/",-1,R_res_ort_txt);
else if prxmatch("/^.*([a-zA-Z]*? kommun).*$/",R_kom_lan) then R_kommun=prxchange("s/^.*?([a-zA-Z]*? kommun).*$/$1/",-1,R_kom_lan);
else */if R_res_ort_txt in kommuner then R_kommun=R_res_ort_txt;
run;
However, for some reason this does not seem to work for all of the municipalities. The municipality of "uppsala" works for instance, but not the municipality of "ängelholm".
I have tried stripping the variables of whitespace and converting everything to lowercase. What am I doing wrong?
Additional info:
For some reason it does work flawlessly if I skip the array and just copy-paste the exact same list of municipality names into a parenthesis following the in-operator. I would however need to repeat this step 5-6 times and this solution would make my code quite cumbersome.

You are defining the ARRAY
Array kommuner{290} $
with character length 8. See what happens when you fix that.

Related

What method am I not using right?

I'm new to Julia and currently trying to run the following code:
Using DelimitedFiles
M=readdlm(data)
ts,A=M[:,1],M[:,2:end]
(nsweeps,N)=size(A)
dx=0.01;
x=[minimum(collect(A)):dx:maximum(collect(A))];
bx=[x-dx/2,x[end]+dx/2];
But, when I try to run the last line of code, it gives me the following error:
MethodError: no method
matching(::Array{StepRangeLen{Float64,Base.TwicePrecision
{Float64},Base.TwicePrecision{Float64}},1}, ::Float64)
Closest candidates are:
-(!Matched::BigFloat, ::Union{Float16, Float32, Float64}) at
mpfr.jl:437
-(!Matched::Complex{Bool}, ::Real) at complex.jl:307
-(!Matched::Missing, ::Number) at missing.jl:115
Can you please help me? Also, the data I'm using it's
30×6 Array{Float64,2}
UPDATE here's the whole function I'm trying to run is the following:
function mymain(filename,nsamples)
start_time=time()
M=readdlm(filename)
ts,A=M[:,1],M[:,2:end]
(nsweeps,N)=size(A)
dx=0.01;
x=[minimum(collect(A)):dx:maximum(collect(A))];
bx=[x-dx/2,x[end]+dx/2];
(bx,hA)=hist(A[:],bx);
f1=figure()
subplot(2,1,1); plot(ts,A,"-o"); xlabel("Time [ms]"); ylabel("Amps
[mV]");
subplot(2,1,2); plot(x,hA,"-"); xlabel("Amps [mV]");
ylabel("Density");draw()
nparams=8
Sx=Array(ASCIIString,1,nparams)
Rx=zeros(2,nparams)
nx=zeros(Int,1,nparams)
Sx[1,1]="p"; Rx[1:2,1]=[0.02,0.98]; nx[1]=49
Sx[1,2]="n"; Rx[1:2,2]=[1,20]; nx[2]=20
Sx[1,3]="tD"; Rx[1:2,3]=[50,200]; nx[3]=46
Sx[1,4]="a"; Rx[1:2,4]=[0.05,0.5]; nx[4]=46
Sx[1,5]="siga"; Rx[1:2,5]=[0.01,0.2]; nx[5]=39
Sx[1,6]="sigb"; Rx[1:2,6]=[0.01,0.1]; nx[6]=19
Sx[1,7]="tauf"; Rx[1:2,7]=[50,200]; nx[7]=46
Sx[1,8]="u1"; Rx[1:2,8]=Rx[1:2,1]; nx[8]=nx[1]
x=zeros(maximum(nx),nparams)
p=zeros(maximum(nx),nparams)
dx=zeros(1,nparams)
for j=1:nparams
x[1:nx[j],j]=linspace(Rx[1,j],Rx[2,j],nx[j])'
dx[j]=x[2,j]-x[1,j]
end
S=zeros(Int,nsamples,nparams)
sold=zeros(Int,1,nparams)
for j=1:nparams
sold[j]=rand(1:nx[j])
end
while x[sold[4],4]<=x[sold[5],5]
sold[4]=rand(1:nx[4])
sold[5]=rand(1:nx[5])
end
while x[sold[8],8]<=x[sold[1],1]
sold[1]=rand(1:nx[1])
sold[8]=rand(1:nx[8])
end
xold=zeros(1,nparams)
xnew=zeros(1,nparams)
for j=1:nparams
xold[j]=x[sold[j],j]
end
llold=myloglikelihood(xold,ts,A)
for k=1:nsamples
snew=sold+rand(-1:1,1,nparams)
if all(ones(1,nparams).<=snew.<=nx)
allowed2=x[snew[4],4]>x[snew[5],5]
allowed3=x[snew[8],8]>x[snew[1],1]
if allowed2&allowed3
for j=1:nparams
xnew[j]=x[snew[j],j]
end
llnew=myloglikelihood(xnew,ts,A)
if rand()<exp(llnew-llold)
sold,llold=snew,llnew
end
end
end
S[k,:]=sold
end
for k=1:nsamples
for j=1:nparams
p[S[k,j],j]+=1/(nsamples*dx[j])
end
end
f2=figure()
for j=1:nparams
subplot(2,4,j)
plot(x[1:nx[j],j],p[1:nx[j],j]);
xlabel(Sx[j])
end
diff_time=time()-start_time;
println("Total runtime
",round(diff_time,3),"s=",round(diff_time/60,1),"mins." );
return S
end
This goes in line with some other functions, but as you can see, this is the main function, so I really can't move forward without first runnning this one.
It isn't clear what outcome you are hoping for here. So I'll just give some pointers that hopefully help.
First, in this line:
x=[minimum(collect(A)):dx:maximum(collect(A))];
the calls to collect are redundant. Also, I suspect you are trying to construct a StepRangeLen, but by putting it in [] you actually are getting a Vector{StepRangeLen}. I think what you want in this line is actually this:
x=minimum(A):dx:maximum(A);
Second, in this line:
bx=[x-dx/2,x[end]+dx/2];
note that dx/2 is a Float64 while x is a StepRangeLen. This is important because the latter is a collection so if you want to perform this operation element-wise across the collection you need to broadcast, that is, x .- dx/2. Note, I suspect you may not be on the latest version of Julia, because when I run this the error message actually tells me explicitly I need to broadcast. Anyway, in contrast, x[end]+dx/2 is fine and does not need to be broadcast because x[end] is Float64. So I think you want:
bx=[x .- dx/2, x[end] + dx/2];
Having said that, it isn't clear to me why you want this bx, which is why I said at the start I'm not sure what outcome you were hoping for.

SOLR - Getting all document entries from intersecting polygon of given search request

I'm running a SOLR with a set of OSM Addresses.
On the other hand I have some polygons in the same collection.
I got the JTS in my solr, everything is working fine.
My problem: For example the user is searching for a specific address like this:
/solr/select?q=Bruhweg&wt=json
The result will look like something like this:
"type":["Feature"],
"wkt":["POLYGON((9.0650999 48.80407349974092,9.065108 48.80405349974092,9.0651604 48.804044599740934,9.0651758 48.80405819974092,9.0652663 48.804044699740935,9.0652795 48.804077499740934,9.065298 48.804074799740924,9.06531 48.80411229974091,9.0653315 48.80411249974092,9.0653495 48.804152299740906,9.0653595 48.804185299740894,9.065227 48.8042043997409,9.0652154 48.804206599740894,9.0652046 48.80417829974089,9.0651805 48.8041809997409,9.065163500000002 48.8041373997409,9.0651464 48.8041391997409,9.0651335 48.80409639974092,9.0651051 48.80409789974092,9.0650999 48.80407349974092))"],
"properties.label":"Bruhweg 11"
"properties.category":"address"
where the field "wkt" is matched to the correct SOLR Type, which we can simply verify with the following request:
solr/select?fq=wkt%3A%22Intersects(9.0650999%2048.80407349974092)%22&q=&wt=json*
Notice that I have just taken the first coordinate from "Bruhweg 11" polygon. The result looks like this:
"type":["Feature"],
"wkt":["POLYGON((9.0650999 48.80407349974092,9.065108 48.80405349974092,9.0651604 48.804044599740934,9.0651758 48.80405819974092,9.0652663 48.804044699740935,9.0652795 48.804077499740934,9.065298 48.804074799740924,9.06531 48.80411229974091,9.0653315 48.80411249974092,9.0653495 48.804152299740906,9.0653595 48.804185299740894,9.065227 48.8042043997409,9.0652154 48.804206599740894,9.0652046 48.80417829974089,9.0651805 48.8041809997409,9.065163500000002 48.8041373997409,9.0651464 48.8041391997409,9.0651335 48.80409639974092,9.0651051 48.80409789974092,9.0650999 48.80407349974092))"],
"properties.label":"Bruhweg 11"
"properties.category":"address"
"type":["Feature"],
"wkt":["POLYGON((9.04226364394244 48.78375351560371,9.056374594613018 48.78376897799828,9.059599869694331 48.78163342420202,9.081809522348271 48.78162969553386,9.081761686040636 48.78226547394644,9.082034238895682 48.78253237030596,9.0856383112481 48.783065728036505,9.081013408886774 48.784895520573016,9.08026549061692 48.78523860978792,9.075279465525824 48.78709720944531,9.075385415333642 48.78784623302399,9.075233988916805 48.78855044494239,9.07538936442188 48.789573632543714,9.076086792392704 48.7899895067132,9.076400978454831 48.79076807795856,9.076954863544922 48.791675940101136,9.077323565779931 48.79247426327167,9.077661454940172 48.79359088232947,9.07820886975919 48.79468135994337,9.078817709274189 48.79498392847467,9.079201898938019 48.79520667445914,9.08016333903955 48.79554857516962,9.079778914425672 48.79518198750416,9.079578091974522 48.79467583486249,9.07960878146339 48.79427927263826,9.079777474351143 48.794273758443296,9.079890211573053 48.79495800149708,9.08018722352202 48.795156515552904,9.080499405509059 48.795473674672415,9.080691862967708 48.795850379952874,9.080924981851217 48.79602374816294,9.081213923069749 48.79636161227024,9.08216503129647 48.797025378100585,9.082445049467385 48.796812064430696,9.082997510785853 48.79666782293686,9.083016532890815 48.798210792078066,9.084546369553149 48.79818997904426,9.08436196313672 48.79857684935421,9.084539100024378 48.798725962114545,9.084861025057869 48.79994602380419,9.084990671623789 48.80091802856458,9.08494424868377 48.80165186074672,9.085041682576207 48.80209339903589,9.08538756537159 48.802966378021715,9.086053469766966 48.80390370812341,9.085797560540206 48.803894003364,9.085814723600494 48.80437509265621,9.085407910580006 48.80612600144947,9.086188011009677 48.80771257109675,9.087281863093846 48.80971073096298,9.086304168618957 48.809607114458665,9.08560710806074 48.80961663014216,9.084061519923088 48.80938486330555,9.084077207035987 48.80980658042419,9.083543243778244 48.811250273222925,9.056400173581295 48.81120764569635,9.055584833918676 48.81131770190693,9.05233070753251 48.81010351728196,9.050449180583943 48.81143341235964,9.047719936766072 48.81048226708057,9.047480579846537 48.81086460229471,9.04435778697807 48.809744505510096,9.042626974936422 48.809303638995836,9.038908229735283 48.80850647660382,9.037144908448282 48.808184194812405,9.038129032787538 48.805980772861595,9.038328485640509 48.8051777307488,9.03824791543268 48.80471104904327,9.03876113924191 48.80479984728909,9.03889592317821 48.802984244443905,9.037701980314722 48.802949557020376,9.036369132836748 48.80272688628704,9.035368430087201 48.802290085608014,9.035368259304592 48.8020471956323,9.033685064639178 48.801228620567926,9.032644752493526 48.800737922085965,9.03217057774127 48.8000889091118,9.032098034258505 48.799508062093246,9.031417252535123 48.79921048432656,9.031802215987176 48.798784151506815,9.031441019924467 48.798238377399365,9.03225196905176 48.797687782137466,9.033547957749326 48.79595641436032,9.038695453795164 48.79394510865497,9.03934415364024 48.7933001500083,9.041676969957162 48.7917058788909,9.043068750531523 48.789672193708604,9.043444520390391 48.788257529857816,9.043555307907576 48.787275490180576,9.042249027750703 48.785802079685375,9.042512195810728 48.784854079302804,9.042199907427747 48.7840458175582,9.04226364394244 48.78375351560371))"],
"properties.label":"polygonA",
"properties.category":"polygons"
What I want:
If the user is just searching for Bruhweg I want as result the address AND the polygonA from category polygons
Any Idea how to solve this?

Adding an array to an existing csv file

I know this question was already asked, but after trying most of the accepted answer none of them seems to work with my simple task...
I have a csv file as follow:
Date,Median
2000-01-31,9
2000-02-28,8
2000-03-31,7
2000-04-30,6
2000-05-31,5
2000-06-30,4
2000-07-31,3
2000-08-31,2
2000-09-30,1
2000-10-31,0
2000-11-30,11
2000-12-31,12
and then an array:
[0.1829 0.171349 0.162461 0.152306 0.14122 0.137749 0.138802 0.150315
0.156784 0.168297 0.180634 0.187241]
I wish to append this array as a third column to the csv file to get the following output:
Date,Median,Median2
2000-01-31,9,0.1829
2000-02-28,8,0.171349
2000-03-31,7,0.162461
2000-04-30,6,0.152306
2000-05-31,5,0.14122
2000-06-30,4,0.137749
2000-07-31,3,0.138802
2000-08-31,2,0.150315
2000-09-30,1,0.156784
2000-10-31,0,0.168297
2000-11-30,11,0.180634
2000-12-31,12,0.187241
I tried most of the answer related to this kind of question but I did not succeed to make them work..here is the last code I tried, using pandas that looks easier but it does not work:
data=pd.read_csv("data_1.csv",sep=',')
array_transpose = array.reshape((-1, 1)) #in order to transpose the array
data['Median2'] = data[array_transpose]
data.to_csv('output.csv')
which produce the following error:
KeyError: '[0.1829 0.171349 0.162461 0.152306 0.14122 0.137749 0.138802 0.150315\n 0.156784 0.168297 0.180634 0.187241] not in index'
How to append this array to my csv file?
You may not need reshape
data=pd.read_csv("data_1.csv",sep=',')
data['Median2'] = array
data.to_csv('output.csv')

many array element and i need to search a file and print array whcih are not present in the file and there should be no duplicate records in my output

Please help me on the below code :
I have an array with 155 elements and i have a file which has some elements of array inside it , i need all values of the array elements which are found in the file and also i need the array element to be printed as zero if the array element is not found in the file .
Thanks in advance, this is what i have tried.
args=("C9" "DP10" "DP11" "DP20" "DP21" "DP30" "DP31" "DP50" "FR31" "G128" "G402" "G602" "GA" "GI" "GT08" "GT14" "GT17" "GT25" "GT37" "GT67" "H6" "H7" "IL" "IM" "J6" "JD05" "JD09" "JD14" "JD25" "JD37" "K1" "K2" "L100" "L106" "L116" "L150" "L202" "L7" "L8" "L9" "LD11" "LD21" "LE09" "LE26" "LP11" "LP21" "LP31" "LP55" "LQ11" "LQ21" "LQ31" "LS07" "LT09" "LT10" "LT12" "LT15" "LT20" "LT22" "LT24" "LT25" "LT30" "LT38" "LT42" "LT43" "LT44" "LT48" "LT50" "LT59" "LT60" "LT65" "M395" "OV04" "OV07" "OV14" "OV18" "OV23" "OV27" "OV35" "OV39" "OV40" "OV79" "Q15" "Q150" "Q19" "QD11" "QD21" "QD31" "QD65" "QE11" "QE21" "QE31" "QF50" "QM25" "QP10" "QP15" "QP20" "QP30" "QP31" "QP50" "QT25" "QT50" "R39" "R40" "r57" "R9" "rc23" "RC27" "RC39" "rc7" "rc79" "S1" "S101" "S117" "S118" "S13p" "S18" "S202" "S317" "S318" "S319" "S40" "S408" "S67" "S76" "S82" "S99" "SD11" "SD12" "SD14" "SD17" "SD29" "SD3" "SD5" "SD98" "SF20" "SF74" "SR07" "SV19" "SV6p" "T402" "T602" "TG00" "TG17" "TG43" "TG8" "TG92" "WD09" "WD14" "WD17" "WD24" "WD29" "WD37" "WD43" "WWE1" "XR91")
MY CODE :
for loop i have used to traverse the elements search inside a file .
for i in ${args[#]}; do
grep $i file.txt
if [ $? -ne 0 ]; then
echo $i"","""0"
fi
done >> output.txt
TOTAL FILE:
C9,5015319
DP10,36870732
DP11,188
DP20,18728254
DP21,341182
DP30,8415555
DP31,2390000
DP50,12371853
FR31,24541
G128,49780
G402,2000
G602,2000
GA,879888
GT08,1580384
GT17,1968192
GT25,4104
GT37,21550
GT67,24770
H6,660652
IL,137651
JD05,1518400
JD14,325800
JD25,828600
JD37,357100
K1,261549
K2,4715330
L100,284
L116,80000
L7,200847
L8,3158
L9,5054495
LE09,75776
LE26,343410
LP11,1030
LP21,492
LP31,113
LP55,3
LQ11,6776000
LQ21,3543600
LQ31,4525600
LT09,682800
LT12,5715
LT15,568873
LT22,236077
LT24,702800
LT25,4600
LT38,28990
LT65,300125
M395,29600
OV14,462
OV18,86300
OV40,217899
Q150,678
QD11,1000022
QD31,50
QF50,58575
QM25,57900
QP10,1792153
QP15,953400
QP20,770000
QP30,179450
QP31,163223
QP50,8
QT50,66340
R39,62440
R40,18807
r57,3456
rc23,3370
RC27,2809
RC39,2570
rc7,7137
rc79,1296
S1,25007
S117,1000000
S13p,52313
S18,75000
S317,289148
S318,3046
S319,30000
S40,300
S408,4967
S76,28
S82,103238
S99,480
SD11,6719
SD12,23123
SD14,22595
SD17,100000
SD29,252392
SD3,20000
SD5,14090
SD98,653
SF20,1000
SF74,7330
SV19,26461
SV6p,154994
T402,2000
T602,2000
TG17,2031
TG8,2964
TG92,1759
WD17,131194
WD24,94589
WD29,202198
WD37,101794
WD43,112942
WWE1,9600
XR91,70000
EXPECTED OUTPUT :
The output should contain the values which are present in the file for each array element.
If not present the output should contain the array element as zero. For eg:
c9 is not present in the file
output of c9 should be
c9,0
Your approach is not bad. I just would use
^$i,
as a grep-pattern. With your current file data, it's not necessary, but maybe one day your file will contain things like
X,2354
XA,1234
and suddenly your algorithm will fail, if args contain the element X.
Also, the echo statement is unnecessarily complex. I would write it simply as
echo $x,0
You can also simplify the if, by combining it with the grep
if ! grep ^$i, file.txt
but this is mere cosmetics and a matter of taste.

Why can't my yacc rule reduce here?

I am using YACC to do my compiler homework project. I found that my program could not get the syntax tree. So I printed it all out to see what is happening. According to my result, it seems that ClassDecl does not reduce to ClassDeclList here. But I can't understand why... can anyone help me out?
The sample input is:
program ex11;
class ab {
}
It printed out as:
programXXXX ex11ID
semicon abID
RBRACEnum
ClassBody ClassDecl ClassDecl1 Error!
The first three lines are messages I printed from my LEX file, to ensure that the characters are recognized correctly.
According to the information, the parser successfully reduces {} to ClassBody and class ab {} to ClassDecl. And then it does not reduce to ClassDeclList, is it because I am writing a left recursive grammar here?
This is the part of my YACC rule base for the inference:
Program: PROGRAMnum IDnum SEMInum ClassDeclList
{printf("program"); $$ = MakeTree(ProgramOp,$4, MakeLeaf(IDNode,$2)); printtree($$,0);};
ClassDeclList: ClassDecl
{printf("ClassDeclList1");$$ = MakeTree(ClassOp,NullExp(),$1); printf("ClassDeclListend");};
|ClassDecl ClassDeclList
{printf("ClassDeclList2");$$ = MakeTree(ClassOp,$2,$1); printf("ClassDeclList");};
ClassDecl: CLASSnum IDnum ClassBody
{printf("ClassDecl");$$=MakeTree(ClassDefOp,$3,MakeLeaf(IDNode,$2)); printf("ClassDecl1");};
Have you tried
| ClassDeclList ClassDecl
instead of
| ClassDecl ClassDeclList
?
I remember this fixing many problems when I used to use CUP.

Resources