Below is the syntax and semantics for a single Boolean expression followed by a
ID: 3804108 • Letter: B
Question
Below is the syntax and semantics for a single Boolean expression followed by a period. Write a program which prompts the user to input a file name which contains the Boolean expression or simply to input the string to be checked (indicate which input method you will use in your comments). You may assume that no input will be longer than 100 characters in length. Expressions may contain white spaces and white spaces should be considered to be delimiters (i.e. a white space between the and > of the implication symbol would be a syntax error). The program should check if the expression in the file is of valid syntax and (if valid) compute the value of the expression. The output should either be an error message or a a message that gives the value of the expression. You must use the techniques taught in the class this is a recursive descent interpreter.
Syntax: (note: for use the lowercase letter ”v” and for use the caret symbol)
Selection Sets
B ::= IT . {~,T,F,(}
IT ::= IT IT_Tail {~,T,F,(}
IT_Tail ::= > OT IT_Tail { >}
::= {.,)}
OT ::= AT OT_Tail {,T,F,(}
OT_Tail ::= AT OT_Tail {}
::= { >,.,)}
AT ::= L AT_Tail {,T,F,(}
AT_Tail ::= L AT_Tail {}
::= {, >,.,)}
L ::= A {T,F,(}
::= ~ L {~}
A ::= T {T}
::= F {F}
::= ( IT ) {(}
Syntactic Domains:
B : Bool stmt
IT : Imply term OT : Or term
AT : And term
IT Tail : Imply tail
OT Tail : Or tail
AT Tail : And tail
L : Literal
A : Atom
Semantic Domain:
b = {T.F} (Boolean values True and False)
Semantic Function Domains:
:Boolstmt b
:Implyterm b
:Orterm b
:Andterm b
:b×Implytail b
:b×Ortail b
:b×Andtail b
: Literal b
:Atom b
Semantic Equations:
( IT. ) = ( IT )
( OTIT Tail ) = (( OT ), IT Tail )
( ATOT Tail ) = (( AT ), OT Tail )
(LAT Tail)=((L),AT Tail)
(b,) = b (where b {T,F})
(F, > OTIT Tail ) = (T, IT Tail )
(T, > OTIT Tail ) = (( OT ), IT Tail )
(b,) = b (where b {T,F})
(T, ATOT Tail ) = T
(F, ”ATOT Tail ) = (( AT ), OT Tail )
(b,) = b (where b {T,F})
(F,LAT Tail)=F
(T, LAT Tail ) = (( L ), AT Tail ) (L)=if(L)=T thenF elseif(L)=F thenT ( A ) = ( A )
(T )=T
(F )=F
( (IT) ) = (( IT ))
Explanation / Answer
import java.text.*;
/** This category implements a Hidden Markov Model, as well as
the Baum-Welch algorithmic program for coaching HMMs.
@author Holger Wunsch (wunsch@sfs.nphil.uni-tuebingen.de)
*/
public category HMM variety of states */
public int numStates;
/** size of output vocabulary */
public int sigmaSize;
/** initial state chances */
public double pi[];
/** transition chances */
public double a[][];
/** emission chances */
public double b[][];
/** initializes associate HMM.
@param numStates range of states
@param sigmaSize size of output vocabulary
*/
public HMM(int numStates, int sigmaSize)
/** implementation of the Baum-Welch algorithmic program for HMMs.
@param o the coaching set
@param steps the quantity of steps
*/
public void train(int[] o, int steps) {
int T = o.length;
double[][] fwd;
double[][] bwd;
double pi1[] = new double[numStates];
double a1[][] = new double[numStates][numStates];
double b1[][] = new double[numStates][sigmaSize];
for (int s = 0; s < steps; s++) {
/* calculation of Forward- und Backward Variables from the
current model */
fwd = forwardProc(o);
bwd = backwardProc(o);
/* re-estimation of initial state chances */
for (int i = 0; i < numStates; i++)
pi1[i] = gamma(i, 0, o, fwd, bwd);
/* re-estimation of transition chances */
for (int i = 0; i < numStates; i++)
a1[i][j] = divide(num, denom);
}
}
/* re-estimation of emission chances */
for (int i = 0; i < numStates; i++) one : 0);
denom += g;
}
b1[i][k] = divide(num, denom);
}
}
pi = pi1;
a = a1;
b = b1;
}
}
/** calculation of Forward-Variables f(i,t) for state i at time
t for output sequence O with the present HMM parameters
@param o the output sequence O
@return associate array f(i,t) over states and times, containing
the Forward-variables.
*/
public double[][] forwardProc(int[] o) format (time 0) */
for (int i = 0; i < numStates; i++)
fwd[i][0] = pi[i] * b[i][o[0]];
/* induction */
for (int t = 0; t <= T-2; t++)
}
come back fwd;
}
/** calculation of Backward-Variables b(i,t) for state i at time
t for output sequence O with the present HMM parameters
@param o the output sequence O
@return associate array b(i,t) over states and times, containing
the Backward-Variables.
*/
public double[][] backwardProc(int[] o) low-level formatting (time 0) */
for (int i = 0; i < numStates; i++)
bwd[i][T-1] = 1;
/* induction */
for (int t = T - 2; t >= 0; t--)
}
come back bwd;
}
/** calculation of chance P(X_t = s_i, X_t+1 = s_j | O, m).
@param t time t
@param i the quantity of state s_i
@param j the quantity of state s_j
@param o associate output sequence o
@param fwd the Forward-Variables for o
@param bwd the Backward-Variables for o
@return P
*/
public double p(int t, int i, int j, int[] o, double[][] fwd, double[][] bwd) {
double num;
if (t == o.length - 1)
num = fwd[i][t] * a[i][j];
else
num = fwd[i][t] * a[i][j] * b[j][o[t+1]] * bwd[j][t+1];
double denom = 0;
for (int k = 0; k < numStates; k++)
denom += (fwd[k][t] * bwd[k][t]);
come back divide(num, denom);
}
/** computes gamma(i, t) */
public double gamma(int i, int t, int[] o, double[][] fwd, double[][] bwd) {
double num = fwd[i][t] * bwd[i][t];
double denom = 0;
for (int j = 0; j < numStates; j++)
denom += fwd[j][t] * bwd[j][t];
come back divide(num, denom);
}
/** prints all the parameters of associate HMM */
public void print()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.