Syntactical analysis with Flex/Bison part 2 - c++

Hallo,
I need help in Lex/Yacc Programming. I wrote a compiler for a syntactical analysis for inputs of many statements. Now i have a special problem.
In case of an Input the compiler gives the right output, which statement is uses, constant operator or a jmp instructor to which label, now i have to write so, if now a if statement comes, first the first command (before the else) must be give out when the assignment of the if is yes then it must jump to the end because the command after the else isnt needed, so after this jmp then the second command must be give out. I show it in an example maybe you understand what i mean.
Input adr. Output
if(x==0) 10 if(x==0)
Wait 5 20 WAIT 5
else 30 JMP 50
Wait 1 40 WAIT 1
end 50 END
like so. I have an idea, maybe i can do it whith a special if statement like
IF exp jmp_stmt_end stmt_seq END
when the if statement is given in the input the compiler has to recognize the end ofthe statement and like my jmp_stmt in my compiler ( you have to download the files from http://bitbucket.org/matrix/changed-tiny) only to jump to the end. I hope you understand my problem.thanks.

I would do this by a two-stage output: the first pass wold generate a list with each output statement, where the jump targets are encoded by labels, and a second pass, where this list is used to generate the real output. Something like this:
pass one:
Number Label Satatement
10 if(x==0)
20 WAIT 5
30 JMP (A)
40 WAIT 1
50 A END

Related

My loop is not adding anything to my list

Within my program for my TI 83 Plus, I have a loop that is supposed to add random integers to my list, yet it is not working, here is my code:
0->I
While I<10
I+1->I
augment(⌞X,{RandInt(0,52))})
End
Disp (⌞X)
Pause
The program ends up just printing {0} Can anyone tell me where I'm going wrong?

What value does an undeclared variable get in FORTRAN77

I am working with an old fortran77 script where a variable starting with the letter 'I' (indicating it is an integer) is used in a IF-statement, like
IF(I.EQ.1) CALL FUNC
without first being declared and a value being passed to it. So my question is what value does an undeclared integer get? The most logical seem to be 0 or 1. Thanks in advice!
Okay so this is pretty much how the routine works:
100 DO 10 J=1,20
doing stuff
IF (I.EQ.1) CALL FUNC
10 CONTINUE
200 DO 20 J=1,30
doing other stuff
IF (X.GT.0) Y = 1
20 CONTINUE
IF (Y.GT.0) GOTO 100
I = 1
GOTO 100
And as you can see the I is first defined after the two DO-loops. So how does this work the first time when the DO-loop 10 is executed.

unclassifiable statement at (1) Fortran Error

at line 64 confirmed as the errors began
I was asked to modify a legacy code. I have found these errors when compiling error compiling picture . Does anybody know how to solve these errors? I use gfortran as my compiler.
The source code:
* Spectral analysis of measured data *
PARAMETER (ND=86400,NSP=43200,NND=86400)
COMMON /WDATA/ WD(NND),WD2(NND)
COMMON /SPEC/ WSP(NSP)
COMMON /TSDATA/ TS(ND*2),CTTS(ND*2)
COMMON /SPDATA/ P(NSP),DF
REAL MEAN
DATA DT/1.0/
DATA COTL/14400.0/
DATA COTS/600.0/
PI=3.141593
OPEN(32,FILE="Pw.txt",STATUS="OLD")
OPEN(12,FILE="output1",STATUS="UNKNOWN")
OPEN(13,FILE="output2",STATUS="UNKNOWN")
DO J=1,NND
READ(32,*)WD(J)
END DO
TOTAL=0.0
MEAN=0.0
DO J=1,NND
TOTAL=TOTAL+WD(J)
END DO
MEAN=TOTAL/FLOAT(NND)
DO J=1,NND
WD(J)=WD(J)-MEAN
END DO
Numerical filtering and Spectral analysis
M=ND/2
KF=1
TD=DT*FLOAT(ND)
DF=1./TD
DO J=1,ND
TS(J)=WD(J)
TS(J+ND)=0.
END DO
COFL=1./COTL
COFH=1./COTS
NCUTL=IFIX((COFL+DF/2.)/DF)+1
NCUTH=IFIX((COFH-DF/2.)/DF)+1
=========================
CALL CUTOFF(M,NCUTL,NCUTH)
=========================
DO J=1,ND
WD2(J)=CTTS(J)
END DO
=================================
SUBROUTINE CUTOFF(M,NCUTL,NCUTH)
=================================
PARAMETER(ND=86400,NSP=43200)
COMMON /TSDATA/ TS(ND*2),CTTS(ND*2)
COMMON /FFTDATA/ W1(ND*2)
MM=M+M
M4=MM+MM
DO J=1,MM
W1(2*J-1)=TS(J)
W1(2*J)=TS(J+MM)
END DO
===============
CALL FOUR1(MM,1)
===============
DO J=1,M
IF(J.EQ.1.AND.NCUTL.GT.0)THEN
W1(1)=0.
W1(2)=0.
ELSE IF(J.LT.NCUTL)THEN
W1(2*J-1)=0.
W1(2*J)=0.
W1(M4-2*J+3)=0.
W1(M4-2*J+4)=0.
END IF
IF(J.GT.NCUTH)THEN
W1(2*J-1)=0.
W1(2*J)=0.
W1(M4-2*J+3)=0.
W1(M4-2*J+4)=0.
END IF
IF(NCUTH.GT.M) THEN
W1(MM+1)=0.
W1(MM+2)=0.
END IF
END DO
-----------------
CALL FOUR1(MM,-1)
-----------------
DO I=1,MM
CTTS(I)=W1(2*I-1)/FLOAT(MM)
CTTS(I+MM)=W1(2*I)/FLOAT(MM)
END DO
RETURN
END
==========================
SUBROUTINE FOUR1(NN,ISIGN)
==========================
PARAMETER(ND=86400)
REAL*8 WR,WI,WPR,WPI,WTEMP,THETA
COMMON /FFTDATA/ DATA(ND*2)C
N=2*NN
J=1
DO 11 I=1,N,2
IF(J.GT.I) THEN
TEMPR=DATA(J)
TEMPI=DATA(J+1)
DATA(J)=DATA(I)
DATA(J+1)=DATA(I+1)
DATA(I)=TEMPR
DATA(I+1)=TEMPI
ENDIF
M=N/2
1 IF ((M.GE.2).AND.(J.GT.M)) THEN
J=J-M
M=M/2
GO TO 1
ENDIF
J=J+M
11 CONTINUE
MMAX=2
2 IF (N.GT.MMAX) THEN
ISTEP=2*MMAX
THETA=6.28318530717959D0/(ISIGN*MMAX)
WPR=-2.D0*DSIN(0.5D0*THETA)**2
WPI=DSIN(THETA)
WR=1.D0
WI=0.D0
DO 13 M=1,MMAX,2
DO 12 I=M,N,ISTEP
J=I+MMAX
TEMPR=SNGL(WR)*DATA(J)-SNGL(WI)*DATA(J+1)
TEMPI=SNGL(WR)*DATA(J+1)+SNGL(WI)*DATA(J)
DATA(J)=DATA(I)-TEMPR
DATA(J+1)=DATA(I+1)-TEMPI
DATA(I)=DATA(I)+TEMPR
DATA(I+1)=DATA(I+1)+TEMPI
12 CONTINUE
WTEMP=WR
WR=WR*WPR-WI*WPI+WR
WI=WI*WPR+WTEMP*WPI+WI
13 CONTINUE
MMAX=ISTEP
GO TO 2
ENDIF
RETURN
END
You haven't closed the main program with an end statement before the subroutine cutoff statement
DO J=1,ND
WD2(J)=CTTS(J)
END DO
=================================
SUBROUTINE CUTOFF(M,NCUTL,NCUTH)
=================================
This should read something like
DO J=1,ND
WD2(J)=CTTS(J)
END DO
END
SUBROUTINE CUTOFF(M,NCUTL,NCUTH)
however that doesn't really make sense. I'm sure there are more missing lines. There are also many illegal statements in the code presented mostly due to bad formatting as Vladimir F has noted.
Muhajjir,
If gfortran is anything like fortran used to be, this code will generate a
plethora of errors. One thing is for sure, there does HAVE to be and END statment at the end of the main. Otherwise, the compiler gets VERY confused. In
addition, some of your statments appear to have code to the left of column 7.
Remember, fortran dates from the days of IBM punch cards, which were HIGHLY
column oriented. A capital 'c' was typically used, in column 1, to indicate a
comment. Column 6 was reserved for continuation character, which eventually
became any character you wanted, as long as column 6 was not empty. Numbers used as labels HAD to start in column 1 and could not go past column 5 into 6, or beyond. This code looks like many of these basic rules have been violated. Check everything, straighten it all out, and everythng should be fine. If not, we can go from there.
Blake Mitchell
Retired free lance programmer
P.S. To whom it may concern,
I have read through the how to answer link and this answer appears to fit the
requirements perfectly. Why do you think it doesn't?

Bug in a fixed If - Else statement

this is my first post in this site.
As the title says, I encountered a bug in an If - else if - else block of codes.
What the problem is that, I am pretty confident that I have covered all 9 possible outcomes that there should be.
It is quite hard to explain more, so if you guys could take a look at my .cpp file and run it ( hopefully it runs because I used c++ on a mac), you may find an easier understanding of my problem.
source code cpp file
If you browse through my action();, in the else statement, I purposely displayed that you encountered a bug so just in case there were bugs, I'd be informed.
What I am trying to do is like this:
userinput | randomAImove | outcome
A 1 statement
A 2 statement
A 3 statement
D 1 statement
D 2 statement
D 3 statement
W 1 statement
W 2 statement
W 3 statement
else 1||2||3 statement
There are corresponding statements to each conditions met.
'A', 'W', 'D' are user input. I used a toupper command to force the compiler to just check on 'A' 'W' 'D'. However, the main problem I find is that, even if the toupper works(which I confirmed), the program displays the correct statement often but still somehow manages to bug even if the user(I tried it) input from A, W, D (not-case sensitive cuz of toupper).
Please just try it and after a few tries you may encounter it also. I wrote "you encountered a bug" if you ever do encounter it.
Please help me because I can't really see any other way to find the mistake. Maybe its in my plain sight but I can't seem to notice which one is it.
Thank you!
Change the AImove function to
void AImove()
{
turn = rand () % 3 + 1;
}
(add + 1), or you may get 0 in turn and it will lead you "encounter a bug".

Rainflow algorithm - Fortran conversion to Matlab

I am trying to convert a Rainflow cycle counting algorithm which is in Fortran, which is a language I am not familiar with, into Matlab.
There is a ready made Rainflow I've downloaded for Matlab but that does not fit the requirements of my project so I'm trying to build one from scratch.
Here is the Fortran code:
INTEGER BUFFER (4096), INDEX, VALUE, RANGE, MEAN, X, Y
INDEX = 0
10 CONTINUE
call 'get next peak/valley', VALUE
INDEX = INDEX + 1
BUFFER (INDEX) = VALUE
20 CONTINUE
IF (INDEX.LT.3) THEN
not enough points to form a cycle
GOTO 10
ELSE
X = ABS (BUFFER(INDEX) - BUFFER(INDEX - 1))
Y = ABS (BUFFER(INDEX - 1) - BUFFER(INDEX - 2))
IF (X.GE.Y) THEN
c -- cycle has been closed
RANGE = Y
MEAN = (BUFFER(INDEX-1) + BUFFER(INDEX-2))/2
c -- remove the cycle
INDEX = INDEX - 2
BUFFER(INDEX) = BUFFER(INDEX+2)
c -- see if this value closes any more cycles
GOTO 20
ELSE
GOTO 10
END IF
END IF
I had downloaded f2matlab (a Fortran to Matlab converter) but it requires a Fortran compiler which I do not have.
The bits I don't really understand how I can convert are:
The call 'get next… line (is this an input()?)
The BUFFER(4096) etc (is this a bit large to be a matrix in matlab?)
The GOTO/CONTINUE structure.
What do they mean, in English (or Matlab)?
I have seen
How to translate fortran goto state to matlab
and
translating loop from Fortran to MATLAB
but they do not help me very much.
This
call 'get next peak/valley', VALUE
isn't (currently) syntactically valid Fortran and I'm not sure whether any compiler of yore would have understood it either. I guess that it means get a VALUE for use in the following bits of code.
INTEGER BUFFER (4096)
is a simple declaration that BUFFER is a vector of 4096 integers, nothing to scare Matlab in that volume of data.
Finally, GOTO is an unconditional jump and the number following it is the label of the line to jump to, so GOTO 10 means execute the line with label 10 next. It was fairly common in FORTRAN of the vintage you are showing us to jump to a CONTINUE statement which is, in this context, a no-operation, execution continues to the next line.
In another context, with DO loops CONTINUE would have marked the end of the block of code inside the scope of the loop and would have a subtly different effect.