Pages: [1]   Go Down
Print
Author Topic: Interprete di VSL  (Read 1281 times)
0 Members e 1 Utente non registrato stanno visualizzando questa discussione.
Etienne
Matricola
*
Offline Offline

Posts: 1


« on: 19-01-2019, 00:54:51 »

Ho realizzato (in linguaggio C++) un interprete del linguaggio VSL (e dunque della relativa macchina astratta) nella versione del linguaggio VSL proposta nell'esercizio 4 della sezione Macchine Astratte.

Sembra funzionare correttamente.

Sono cosciente del fatto che analizzare e nel caso correggere tale sorgente è un'avventura tediosa ed impiegherebbe troppo tempo ma...
tentar non nuoce

Ecco il sorgente:

Code:
#include <fstream>
#include <iostream>
#include <string>
#define N 4
#define MAX 100
using namespace std;
//KEYWORDS
//1
const string km ="m[";
//2
const string kq ="]";
//3
const string koperator ="<-";
//4
const string koutput ="output(";
//5
const string kp =")";
//6
const string kinput ="input(";
//7
const string k0 = "0";
//8
const string k1 = "1";
//9
const string k2 = "2";
//10
const string k3 = "3";

//IN AN ARRAY
const string keys []= {"",km,kq,koperator,koutput,kp,kinput,k0,k1,k2,k3} ;
//MACHINE'S MEMORY
class dati{
private:
//DATI
bool m[N];
public:
//DA UTILIZZARE PER L'OUTPUT
getm(short n){
return m[n];
}
//m[INDEX]<-m[INDEX]
setm(short sx,short dx){
m[sx]=m[dx];
}
//m[INDEX]<-0
set0(short sx){
m[sx]=false;
}
//m[INDEX]<-1
set1(short sx){
m[sx]=true;
}
};

//FUNCTIONS INTRODUCTION
bool i_by_file(string);
bool search(string istr[], char s[]);
bool i_typing ();
const short f(string s) ;
bool read_and_do(char [],string[],dati &, unsigned int);
void clear();

//MAIN
int main (){
bool again;
cout<<"Franco Barbanera's VerySimpleLanguage INTERPRETER 2018 vers. 1.0.1 for Windows by Etienne"<<endl<<endl;
//MENU 1
do{
bool okay=true;
bool isbyfile;
{
string a;
do{
cout<<endl<<"How do you want to run a program?";
cout<<endl<<"1. From a file.txt";
cout<<endl<<"0. Typing";
cout<<endl<<"Type _ ";
cin>>a;
}while(a[0]!='0'&&a[0]!='1');
if(a[0]=='0') isbyfile=false;
else isbyfile=true;
}
clear();
//FROM A FILE
if(isbyfile){
//GETTING FILE's NAME
cout<<endl<<"Insert the script file's name ( do not type '.txt' )"<<endl;
cout<<"The file has to be in the same directory as VLSi"<<endl;
ifstream l;
string s="";
do{
cout<<"Type _ ";
cin>>s;
l.open(s+".txt");
if(!l.is_open()){
cout<<"File not found, try again!"<<endl;
}
}while(!l.is_open());
l.close();
cout<<endl<<"Running the program..."<<endl;
//RUNNING THE PROGRAM
okay=i_by_file(s);
}
//TYPING
else{
okay= i_typing ();
}
cout<<endl<<"End";
//LAST MENU
{
string a;
do{
cout<<endl<<endl<<"Do you want to run another program?";
cout<<endl<<"1. Yes";
cout<<endl<<"0. No";
cout<<endl<<"Type _ ";
cin>>a;
}while(a[0]!='0'&&a[0]!='1');
if(a[0]=='0') again=false;
else again=true;
clear();
}
}while(again);
return 0;
//END
}

//TYPING
bool i_typing (){
//FALSE UNTIL THEEND
bool end=false;
//TRUE IF THE INSTRUCTION IS CORRECT
bool correct=true;
dati d;
string t="";
cout<<"Type 'End' to exit and do not use spaces!"<<endl;
while(end==false){
if(t!="End"){
string istr[MAX];
char s[MAX];
for (int i=0;i<MAX;i++){
istr[i]="";
s[i]=' ';
}
cout<<endl<<endl<<"Prelude -> ";
cin>>t;
for(int i=0;i<t.length();i++){
s[i]=t[i];
}
if(t!="End"){
correct=read_and_do(s,istr,d,1);
if(!correct){
cout<<"Found a syntactic error in line "<<endl;
cout<<"-> Program cannot do this istruction!";
}
}
}
else{
end=true;
cout<<"All done"<<endl<<"Showing data"<<endl;
for(int i=0;i<N;i++){
cout<<endl<<"Showing m["<<i<<"] = "<<d.getm(i);
}
}
}
}

//BY FILE
bool i_by_file(string titolo){
//FALSE UNTIL THE END
bool end=false;
//TRUE WHEN THE ISTRUCTION IS CORRECT
bool correct=true;
unsigned int line=1;
dati d;
//READING A LINE
ifstream l;
l.open(titolo+".txt");
while(end==false){
//IF IT IS NOT THE END OF THE FILE
if(!l.eof()){
short index [2];
string istr[MAX];
char s[MAX];
for (int i=0;i<MAX;i++){
istr[i]="";
s[i]=' ';
}
l.getline(s,MAX);
correct=read_and_do(s,istr,d,line);
//IF THERE ARE MISTAKES
if(!correct){
end=true;
cout<<"Found a syntactic error in line "<<line<<endl;
cout<<"-> Program cannot run anymore!";
}
line++;
}
else{
end=true;
cout<<"All done"<<endl<<"Showing data"<<endl;
for(int i=0;i<N;i++){
cout<<endl<<"Showing m["<<i<<"] = "<<d.getm(i);
}
}
}
}

//READS A LINE
bool read_and_do(char s [],string istr [], dati &d,unsigned int line){
//SEARCH EVERY KEYWORD IN THE LINE AND PUT THEM IN THE ARRAY ISTR (UCTIONS)
//cout<<endl<<"Line "<<line<<" -> ";
bool okay=search(istr,s);
if(!okay){
return false;
}
int x=0;
//IN THIS PART OF THE PROGRAM I TRY TO UNDERSTAND THE ISTRUCTIONS, IF AN ISTRUCTION IS WRONG THE FUNCTION RETURNS FALSE
{
switch(f(istr[x])){
//f("m[")
case 1:
switch(f(istr[x+2])){
//f("]")
case 2:
switch(f(istr[x+3])){
//f("<-")
case 3:
switch(f(istr[x+4])){
//f("0")
case 7:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
//m[index]<-0
d.set0((short)((istr[x+1])[0])-((short)'0'));
x=x+5;
break;
default:
return false;
break;

}
break;
//f("1")
case 8:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
//m[index]<-1
d.set1((short)((istr[x+1])[0])-((short)'0'));
x=x+5;
break;
default:
return false;
break;
}
break;
//f("m[")
case 1:
switch(f(istr[x+6])){
//f("]")
case 2:
switch(f(istr[x+5])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
//m[index]<-m[index]
d.setm((short)((istr[x+1])[0])-((short)'0'),(short)((istr[x+5])[0])-((short)'0'));
x=x+7;
break;
default:
return false;
break;
}
break;
default:
return false;
}
break;
default:
return false;
break;
}
break;
}
break;
default:
return false;
break;
}
break;
default:
return false;
break;
}
break;
//f("output(")
case 4:
switch(f(istr[x+2])){
//f(")")
case 5:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
cout<<endl<<"Showing m["<<istr[x+1]<<"] = "<<d.getm((short)((istr[x+1])[0])-((short)'0'))<<endl;
x=x+3;
break;
default:
return false;
}
break;
default:
return false;
break;
}
break;
//f("input(")
case 6:
switch(f(istr[x+2])){
//f(")")
case 5:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
char z;
cout<<endl<<"Set m["<<istr[x+1]<<"] = ";
cin>>z;
switch (z){
case '0':
d.set0((short)((istr[x+1])[0])-((short)'0'));
x=x+3;
break;
case '1':
d.set1((short)((istr[x+1])[0])-((short)'0'));
x=x+3;
break;
default:
//ERRORE DELL'UTENTE
break;
}
break;
default:
return false;
}
break;
default:
return false;
break;
}
break;
default:
return false;
break;
}
}
return true;
}

//SEARCH A KEYWORD IN THE  (CHAR S[) AND PUTS IT IN THE ARRAY ISTR
//RETURNS TRUE IF THE LINE IS CORRECT (HAS ONLY KEYWORDS)
bool search(string istr[], char s[]){
int x=0;
//cout<<s<<endl;
int i=0;
while(i<MAX){
bool flag;
for(int k=1;k<11;k++){
if(s[i]==(keys[k])[0]){
flag=true;
for(int j=0;j<keys[k].length();j++){
if(s[i+j]!=(keys[k])[j]){
flag=false;
}
if(flag){
istr[x]=keys[k];
//cout<<"KEYWORD <"<<keys[k]<<"> RECOGNISED"<<endl;
i=i+keys[k].length()-1;
x++;
break;
}
}
if(flag) break;
}
}
if(s[i]!=' '&&!flag) {
cout<<"Found an error in the script since -> "<<s[i]<<endl;
return false;
}
i++;
}
return true;
}

//CLEAR THE SCREEN
void clear(){
system("cls");
cout<<"Franco Barbanera's VerySimpleLanguage INTERPRETER 2018 vers. 1.0.1 for Windows by Etienne"<<endl<<endl;
}

//F
//ASSOCIATE A NUMBER TO A KEYWORD
const short f(string s) {
for(int a=1;a<11;a++){
if(s==keys[a]){
return a;
}
}
return 0;
}

« Last Edit: 31-01-2019, 21:11:44 by ɹǝǝuıƃuǝsɹǝʌǝɹ » Logged
Franco Barbanera
Moderator
Forumista Eroico
*****
Offline Offline

Posts: 3.081



WWW
« Reply #1 on: 19-01-2019, 13:04:07 »

Sarebbe utile strutturare il codice in modo che siano facilmente individuabili le parti
corrispondenti alle varie componenti della macchina astratta,
per esempio l'Interprete.

FB
Logged
ɹǝǝuıƃuǝsɹǝʌǝɹ
Administrator
God of the Forum
*****
Offline Offline

Gender: Male
Posts: 4.475


Più grande è la lotta, e più è glorioso il trionfo


WWW
« Reply #2 on: 31-01-2019, 21:12:36 »

Ho realizzato (in linguaggio C++) un interprete del linguaggio VSL (e dunque della relativa macchina astratta) nella versione del linguaggio VSL proposta nell'esercizio 4 della sezione Macchine Astratte.

Sembra funzionare correttamente.

Sono cosciente del fatto che analizzare e nel caso correggere tale sorgente è un'avventura tediosa ed impiegherebbe troppo tempo ma...
tentar non nuoce

Ecco il sorgente:

Code:
#include <fstream>
#include <iostream>
#include <string>
#define N 4
#define MAX 100
using namespace std;
//KEYWORDS
//1
const string km ="m[";
//2
const string kq ="]";
//3
const string koperator ="<-";
//4
const string koutput ="output(";
//5
const string kp =")";
//6
const string kinput ="input(";
//7
const string k0 = "0";
//8
const string k1 = "1";
//9
const string k2 = "2";
//10
const string k3 = "3";

//IN AN ARRAY
const string keys []= {"",km,kq,koperator,koutput,kp,kinput,k0,k1,k2,k3} ;
//MACHINE'S MEMORY
class dati{
private:
//DATI
bool m[N];
public:
//DA UTILIZZARE PER L'OUTPUT
getm(short n){
return m[n];
}
//m[INDEX]<-m[INDEX]
setm(short sx,short dx){
m[sx]=m[dx];
}
//m[INDEX]<-0
set0(short sx){
m[sx]=false;
}
//m[INDEX]<-1
set1(short sx){
m[sx]=true;
}
};

//FUNCTIONS INTRODUCTION
bool i_by_file(string);
bool search(string istr[], char s[]);
bool i_typing ();
const short f(string s) ;
bool read_and_do(char [],string[],dati &, unsigned int);
void clear();

//MAIN
int main (){
bool again;
cout<<"Franco Barbanera's VerySimpleLanguage INTERPRETER 2018 vers. 1.0.1 for Windows by Etienne"<<endl<<endl;
//MENU 1
do{
bool okay=true;
bool isbyfile;
{
string a;
do{
cout<<endl<<"How do you want to run a program?";
cout<<endl<<"1. From a file.txt";
cout<<endl<<"0. Typing";
cout<<endl<<"Type _ ";
cin>>a;
}while(a[0]!='0'&&a[0]!='1');
if(a[0]=='0') isbyfile=false;
else isbyfile=true;
}
clear();
//FROM A FILE
if(isbyfile){
//GETTING FILE's NAME
cout<<endl<<"Insert the script file's name ( do not type '.txt' )"<<endl;
cout<<"The file has to be in the same directory as VLSi"<<endl;
ifstream l;
string s="";
do{
cout<<"Type _ ";
cin>>s;
l.open(s+".txt");
if(!l.is_open()){
cout<<"File not found, try again!"<<endl;
}
}while(!l.is_open());
l.close();
cout<<endl<<"Running the program..."<<endl;
//RUNNING THE PROGRAM
okay=i_by_file(s);
}
//TYPING
else{
okay= i_typing ();
}
cout<<endl<<"End";
//LAST MENU
{
string a;
do{
cout<<endl<<endl<<"Do you want to run another program?";
cout<<endl<<"1. Yes";
cout<<endl<<"0. No";
cout<<endl<<"Type _ ";
cin>>a;
}while(a[0]!='0'&&a[0]!='1');
if(a[0]=='0') again=false;
else again=true;
clear();
}
}while(again);
return 0;
//END
}

//TYPING
bool i_typing (){
//FALSE UNTIL THEEND
bool end=false;
//TRUE IF THE INSTRUCTION IS CORRECT
bool correct=true;
dati d;
string t="";
cout<<"Type 'End' to exit and do not use spaces!"<<endl;
while(end==false){
if(t!="End"){
string istr[MAX];
char s[MAX];
for (int i=0;i<MAX;i++){
istr[i]="";
s[i]=' ';
}
cout<<endl<<endl<<"Prelude -> ";
cin>>t;
for(int i=0;i<t.length();i++){
s[i]=t[i];
}
if(t!="End"){
correct=read_and_do(s,istr,d,1);
if(!correct){
cout<<"Found a syntactic error in line "<<endl;
cout<<"-> Program cannot do this istruction!";
}
}
}
else{
end=true;
cout<<"All done"<<endl<<"Showing data"<<endl;
for(int i=0;i<N;i++){
cout<<endl<<"Showing m["<<i<<"] = "<<d.getm(i);
}
}
}
}

//BY FILE
bool i_by_file(string titolo){
//FALSE UNTIL THE END
bool end=false;
//TRUE WHEN THE ISTRUCTION IS CORRECT
bool correct=true;
unsigned int line=1;
dati d;
//READING A LINE
ifstream l;
l.open(titolo+".txt");
while(end==false){
//IF IT IS NOT THE END OF THE FILE
if(!l.eof()){
short index [2];
string istr[MAX];
char s[MAX];
for (int i=0;i<MAX;i++){
istr[i]="";
s[i]=' ';
}
l.getline(s,MAX);
correct=read_and_do(s,istr,d,line);
//IF THERE ARE MISTAKES
if(!correct){
end=true;
cout<<"Found a syntactic error in line "<<line<<endl;
cout<<"-> Program cannot run anymore!";
}
line++;
}
else{
end=true;
cout<<"All done"<<endl<<"Showing data"<<endl;
for(int i=0;i<N;i++){
cout<<endl<<"Showing m["<<i<<"] = "<<d.getm(i);
}
}
}
}

//READS A LINE
bool read_and_do(char s [],string istr [], dati &d,unsigned int line){
//SEARCH EVERY KEYWORD IN THE LINE AND PUT THEM IN THE ARRAY ISTR (UCTIONS)
//cout<<endl<<"Line "<<line<<" -> ";
bool okay=search(istr,s);
if(!okay){
return false;
}
int x=0;
//IN THIS PART OF THE PROGRAM I TRY TO UNDERSTAND THE ISTRUCTIONS, IF AN ISTRUCTION IS WRONG THE FUNCTION RETURNS FALSE
{
switch(f(istr[x])){
//f("m[")
case 1:
switch(f(istr[x+2])){
//f("]")
case 2:
switch(f(istr[x+3])){
//f("<-")
case 3:
switch(f(istr[x+4])){
//f("0")
case 7:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
//m[index]<-0
d.set0((short)((istr[x+1])[0])-((short)'0'));
x=x+5;
break;
default:
return false;
break;

}
break;
//f("1")
case 8:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
//m[index]<-1
d.set1((short)((istr[x+1])[0])-((short)'0'));
x=x+5;
break;
default:
return false;
break;
}
break;
//f("m[")
case 1:
switch(f(istr[x+6])){
//f("]")
case 2:
switch(f(istr[x+5])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
//m[index]<-m[index]
d.setm((short)((istr[x+1])[0])-((short)'0'),(short)((istr[x+5])[0])-((short)'0'));
x=x+7;
break;
default:
return false;
break;
}
break;
default:
return false;
}
break;
default:
return false;
break;
}
break;
}
break;
default:
return false;
break;
}
break;
default:
return false;
break;
}
break;
//f("output(")
case 4:
switch(f(istr[x+2])){
//f(")")
case 5:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
cout<<endl<<"Showing m["<<istr[x+1]<<"] = "<<d.getm((short)((istr[x+1])[0])-((short)'0'))<<endl;
x=x+3;
break;
default:
return false;
}
break;
default:
return false;
break;
}
break;
//f("input(")
case 6:
switch(f(istr[x+2])){
//f(")")
case 5:
switch(f(istr[x+1])){
//f("0")
case 7:
//f("1")
case 8:
//f("2")
case 9:
//f("3")
case 10:
char z;
cout<<endl<<"Set m["<<istr[x+1]<<"] = ";
cin>>z;
switch (z){
case '0':
d.set0((short)((istr[x+1])[0])-((short)'0'));
x=x+3;
break;
case '1':
d.set1((short)((istr[x+1])[0])-((short)'0'));
x=x+3;
break;
default:
//ERRORE DELL'UTENTE
break;
}
break;
default:
return false;
}
break;
default:
return false;
break;
}
break;
default:
return false;
break;
}
}
return true;
}

//SEARCH A KEYWORD IN THE  (CHAR S[) AND PUTS IT IN THE ARRAY ISTR
//RETURNS TRUE IF THE LINE IS CORRECT (HAS ONLY KEYWORDS)
bool search(string istr[], char s[]){
int x=0;
//cout<<s<<endl;
int i=0;
while(i<MAX){
bool flag;
for(int k=1;k<11;k++){
if(s[i]==(keys[k])[0]){
flag=true;
for(int j=0;j<keys[k].length();j++){
if(s[i+j]!=(keys[k])[j]){
flag=false;
}
if(flag){
istr[x]=keys[k];
//cout<<"KEYWORD <"<<keys[k]<<"> RECOGNISED"<<endl;
i=i+keys[k].length()-1;
x++;
break;
}
}
if(flag) break;
}
}
if(s[i]!=' '&&!flag) {
cout<<"Found an error in the script since -> "<<s[i]<<endl;
return false;
}
i++;
}
return true;
}

//CLEAR THE SCREEN
void clear(){
system("cls");
cout<<"Franco Barbanera's VerySimpleLanguage INTERPRETER 2018 vers. 1.0.1 for Windows by Etienne"<<endl<<endl;
}

//F
//ASSOCIATE A NUMBER TO A KEYWORD
const short f(string s) {
for(int a=1;a<11;a++){
if(s==keys[a]){
return a;
}
}
return 0;
}

Ho modificato il messaggio affinché avesse il codice dentro l'opportuno TAG [code], altrimenti diventava illegibile e le [i] diventavano un tag di corsivo...
Logged

La grande marcia della distruzione mentale proseguirà. Tutto verrà negato. Tutto diventerà un credo. È un atteggiamento ragionevole negare l'esistenza delle pietre sulla strada; sarà un dogma religioso affermarla. È una tesi razionale pensare di vivere tutti in un sogno; sarà un esempio di saggezza mistica affermare che siamo tutti svegli. Accenderemo fuochi per testimoniare che due più due fa quattro. Sguaineremo spade per dimostrare che le foglie sono verdi in estate. Non ci resterà quindi che difendere non solo le incredibili virtù e saggezze della vita umana, ma qualcosa di ancora più incredibile: questo immenso, impossibile universo che ci guarda dritto negli occhi. Combatteremo per i prodigi visibili come se fossero invisibili. Guarderemo l'erba e i cieli impossibili con uno strano coraggio. Saremo tra coloro che hanno visto eppure hanno creduto.

In tutto, amare e servire.

  
                            ن                           
I can deal with ads,
I can deal with buffer,
but when ads buffer
I suffer...

...nutrimi, o Signore, "con il pane delle lacrime; dammi, nelle lacrime, copiosa bevanda...

   YouTube 9GAG    anobii  S  Steam T.B.o.I. Wiki [univ] Lezioni private  ʼ  Albo d'Ateneo Unicode 3.0.1
Usa "Search" prima di aprire un post - Scrivi sempre nella sezione giusta - Non spammare - Rispetta gli altri utenti - E ricorda di seguire il Regolamento
Pages: [1]   Go Up
Print
Jump to: