solution8.cpp

来自「lex files for given decription used as 」· C++ 代码 · 共 88 行

CPP
88
字号
//IIT2006021
//Arunanshu Pandey
#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

// Function  for 'S' nontermnal variable
int my_s(string s,int ptr);
int s_production(string s,int ptr);
// Function for 'L' nonterminal variable
int l_production(string s,int pt);

int l_production(string s,int pt)
{
    // Code for the productions given in the rule corresponding to the L non terminal variable   
    int ptr=pt;
    if(s.length()<=ptr)
      return -1;
    else{
         ptr=s_production(s,ptr);
         		if(ptr!=-1)
           			{
            		while(ptr!=-1)
               			{
                				if(s[ptr]==',')
                 					{
                   					ptr=s_production(s,ptr+1);
                 					}
                					else{
                     					return ptr;
                     					}
               			}
            
				return -1;
           		}
         else
            
		return -1;
         
         }
    
}	
int s_production(string s,int ptr)
{
// Code for the productions given in the rule corresponding to the S non terminal variable   
    if(s.length()<=ptr)
       return -1;
    		else{
        		if(s[ptr]=='a')
           			return ptr+1;
        				else if(s[ptr]=='(')
            				{
              					ptr=l_production(s,ptr+1);
              					if(ptr!=-1&&s[ptr]==')')
                					return   ptr+1;
              					else
                					return -1;          
             				}
        				else
           				return -1;
       		}
}

    
 int main(int argc , char *argv[])
{
   string buffer;
    
    cout<<"Enter the string to be checked :";
    cin>>buffer;
    
       // Now , starting with Start Variable 'S' Function
    
    int temp=s_production(buffer,0);
    if((temp!=-1)&&(temp==buffer.length())) 
    cout<<"\n"<<buffer<<" Belongs to the grammar";
    else
    cout<<"\n"<<buffer<<"\n Doesnt Belong to the Grammar";
    
    system("pause");
    return 0;
}
    
    
   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?