PDA

View Full Version : dabadadadadbadada


FrostScYthe
Aug23-04, 04:08 PM
Hi I have a problem for my programming languages class =\

It goes like this We need to make a program that checks the sintaxis for monkeys... =d but well you see their Alphabet is {a, b, d, ^, ;} in where ^ means an empty space.

The grammar is represented in a metalanguage

<sentence> ::= <word>;|<sentence>^<word>
<word> ::= <syllable>|<syllable><word><syllable>
<syllable> ::= <explotion>|<explotion><high>|a<explotion>|a<high>
<high> ::= b|d

the program must answer things like, which of the following is the fake monkey?

Monkey1: ba^ababadada^bad^dabbada;
Monkey2: abdabaadab^ada;
Monkey3: dab^ad^abaadad^badadbaad;

Anyway I've done some work on it but, and I've made the program this way:

import java.io.*;
import java.lang.*;
class Sentence{
private String line; // String de Entrada.
private boolean ValidDigits; // Son los Digitos de Entrada Validos? (a, b, d, ^, ;)
private int location; // Posicion actual en el Arreglo de Caracteres.
private int nmroSilabas; // Numero de Silabas en Palabra.
private char[] Palabra; // Arreglo que toma un fragmento del String;

public Oracion (String s)
{
linea = s;
digValidos = true;
loc = 0;
Palabra = new char[50];
for (int i = 0; i < 50; i++) Palabra[i] = ';';
}

public boolean digitosValidos ()
{
if (linea.charAt(linea.length()-1) != ';')
{
System.out.println("Error: Frase no Finaliza");
return false;
}
for (int i = 0; i < linea.length(); i++)
{
switch (linea.charAt(i))
{
case 'a' : i++;
break;
case 'b' : i++;
break;
case 'd' : i++;
break;
case ';' : return true;

case '^' : i++;
break;
default: return false;
}
}
return true;
}

public boolean esOracion ()
{
while (linea.charAt(loc) != ';')
{ // Ciclo1 que verifica el final de la "Frase".
int j = 0;
while (linea.charAt(loc) != '^')
{ // Ciclo2 que verifica el final de la "Palabra".
Palabra[j] = linea.charAt(loc);
loc = loc + 1; // Aumenta la localizacion en el String.
} // Fin Ciclo2
if (!esPalabra()) System.out.println("Error: Palabra no Valida");
} // Fin Ciclo1
return false;
}

public boolean esPalabra ()
{ // Determina si un fragmento del String dado es una Palabra.
if (nmroSilabas % 2 == 1) return true;
return false;
}

public boolean esSilaba (int pos)
{ // Determina si un segmento del String dado es una silaba.
int aux = pos + 2; // Elemento 2 casillas adelante.
if (esExplosion(pos)) return true;
if (esExplosion(pos) && esAlto(aux)) return true;
if ((linea.charAt(++pos) == 'a') && esExplosion(pos)) return true;
if (esExplosion(pos) && (linea.charAt(++pos) == 'a')) return true;
return false;
}
public boolean esExplosion (int pos)
{ // Determina si un segmento del String dado es Explosion.
if (esAlto(pos) && (linea.charAt(++pos) == 'a')) return true;
return false;
}
public boolean esAlto (int pos)
{ // Determina si un segmento del String es Alto.
if (linea.charAt(pos) == 'b') return true;
if (linea.charAt(pos) == 'd') return true;
return false;
}
}

Aghhhhhhhh too lazy to translate the whole thing but know this
esAlto - isHigh
esExplosion - isExplotion
esSilaba - isSyllable
esPalabra - isWord
esOracion - isSentence

Anyway I'm doing well one checking for High, for Explotion but I GET STUCK HERE:

take: ababadada

Now I can have 4 <explotions>, making 4 syllables which wouldn't make a word IN MONKEY LANGUAGE!

but if I take them as <explotion><high> and a<explostion> I get a VALID WORD IN MONKEY LANGUAGE! because I get 3 syllables.

What's really killing me, it's how to make a function that will check all posible ways to make syllables, and then count them 'til they are an uneven number of syllables which would = word. and ofcourse there will be words that no matter the number of ways you try it, the number of syllables will be even such as "badadbaad" - which makes monkey3 fake monkey.

I hope you get what I'm trying to solve here, any aid would be appreciated.