⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cncparsescan.c

📁 根据数控编程需要
💻 C
字号:
#include"..\ucos-ii\includes.h"               /* uC/OS interface */
#include "..\ucos-ii\add\osaddition.h"
#include "..\inc\FS\OSFile.h"
#include "..\inc\drv.h"

#include <string.h>

#include "..\inc\CNCsoft\CNC.h"
#include "..\inc\CNCsoft\CNCparseGlobals.h"
#include "..\inc\CNCsoft\CNCSHM.h"
#include "..\inc\CNCsoft\CNCparseScan.h"

#include "..\inc\CNCsoft\CNCparseUtil.h"
#include "..\inc\CNCsoft\CNCInit.h"

//extern char FileSaveLine[2000][255];
//extern int FileLineNum[2000];
//extern long StartLineNum;

//extern int ParaBuf[PARAMETER_NUM];
int CommentFlag=1;     //程序选跳功能,1,不选跳,0选跳
int G65Flag=0;        //用于标记该地址的值是常量还是变量表示0:常量1:变量
extern int InitFlag;
//tokenString array stores the lexeme of each token
char tokenString[MAXTOKENLEN+1];

//extern int Decimal_Flag;
extern int ParaBuf[PARAMETER_NUM];
extern int ProgramEndFlag;

extern char TextFilename[5][12];

extern BOOLEAN MDI_Flag;

extern char MDISTR[300];

static char lineBuf[BUFLEN]; //holds the current line
//static int linepos = 0;      //current position in LineBuf
//static int bufsize = 0;      //current size of buffer string

int linepos = 0;      //current position in LineBuf
int bufsize = 0;      //current size of buffer string


extern CNCFILE* InterpFile;

long lineno=-1;     //source line number for listing

int blankno=0;   //标记程序末尾空行数

char tempc=' ';

extern int CNC_ERR;  //报警编号
extern int CNC_IsErr;  //报警状态 1:处理 0:未处理

//TraceScan = TRUE causes token information to be 
//printed to the listing CNCFILE as each token is 
//recoginzed by the scanne
 int TraceScan=1;

//getNextChar fetches the next non-boank character
//from lineBuf, reading in a new line if lineBuf is
//enhausted

int EndFlag=0;  //五行内判断结束标志

int NotingFlag=0;  //注释判断1:是注释 0:不是注释

//index for storing into tokenString
int tokenStringIndex;
//标志tokenStringIndex是否需初始化为零0:需要1:不需要
int tokenStringIndexFlag;
  
  void BlankMissError(long lineno);
  void AddressContinueError(long lineno);
  void IlligalEndingError(long lineno);
  void DecimalError(long lineno);
  
  
  static char getNextChar(void) {
  
	int i;

	if (!(linepos<(bufsize-1))) {
	  lineno++;
	  NotingFlag=0;
	  if (MDI_Flag==0) {
//	  	Uart_Printf("Hi, I am youdongch   1");
	if(InterpFile != NULL)
		  i=LineReadOSFile(InterpFile, lineBuf);    
		else
	{
		CNC_IsErr=1;
		CNC_ERR=1;
	}

//Uart_Printf("ddd %s",lineBuf);
	  }  else {
              lineno--;
              lineBuf[0]=0;
//Uart_Printf("MDISTR= %s\n",MDISTR);
              cncStrNCpy(lineBuf,MDISTR,BUFLEN);
	  	i=strlen(lineBuf);
	  }
         if((i<BUFLEN)&&(i>0)){            
 	        if (lineBuf[i-1]!=10){
		    lineBuf[i]=13;
		    lineBuf[i+1]=10;
		    i=i+2;
		  }
         	};
	
	  if (i>2) {
		 blankno=0;
		  bufsize = i;	//InterpFile(lineBuf);
		  linepos = 0;
  //	   Uart_Printf("lineBuf[0]=%c  ",lineBuf[0]);
		  if (((lineBuf[0]<'A')||(lineBuf[0]>'Z'))&&(lineBuf[0]!='/')&&(lineBuf[0]!=' ')) {
			  CNC_ERR=4;
			  CNC_IsErr=1;
			  }
		  return lineBuf[linepos++];
	  }
	  else if (i>0) {
		  linepos=0;
		  bufsize=0;
		  return '$';
		  }
	  else {
		  return '@';
		  }
	  }
	else { 
	  return lineBuf[linepos++];
	}
  }
  
  
  //ungetNextChar backtracks one character in lineBuf
  static void ungetNextChar(void) {
	linepos--;
  }
  
  //lookup table of reserved words
  static struct {
	char *str;
	TokenType tok;
  } reservedWords[MAXRESERVED]
  = {{"N",N},{"G",G},{"H",H},{"P",P},{"X",X},{"U",U},
	 {"Y",Y},{"V",V},{"Z",Z},{"W",W},{"Q",Q},{"R",R},{"L",L},
	 {"I",I},{"K",K},{"F",F},{"S",S},{"T",T},{"M",M},{"O",O},{"J",J}};
  
  
  //lookup an identifier to see if it is a reserved word
  //use liner search
  static TokenType reservedLookup(char *s) {
	int i;
	for (i=0;i<MAXRESERVED;i++) {
	  if (!strcmp(s,reservedWords[i].str)) {
		return reservedWords[i].tok;
	  }
	}
	return ADDRESS;
  }
  
  
  //the primary function of the scanner
  //function getToken returns the next token in source CNCFILE
  TokenType getToken(void) {
	//index for storing into tokenString
	//int tokenStringIndex;
	//holds current token to be returned
	TokenType currentToken;
	//current state - always begins at START
	StateType state = START;
	//flag to indicate save to tokenString
	int save;
	//hold next char to be returned
	char c;
  
	if (tokenStringIndexFlag==0) {
	  tokenStringIndex=0;
	  }
	else if (tokenStringIndexFlag==1) {
		 tokenStringIndexFlag=0;	  
		 }
	
	EndFlag=0;	  //标志是否是文件结尾
  
	while (state != DONE) {
	  c = getNextChar();
	  if ((((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z')))&&((tempc>='0')&&(tempc<='9'))
		  &&(NotingFlag==0)&&(InitFlag==0)&&(CNC_ERR!=4)) {
		//  BlankMissError(lineno);
		  }
	  if ((((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z')))&&(((tempc>='A')&&(tempc<='Z'))||
		  ((tempc>='a')&&(tempc<='z')))&&(NotingFlag==0)&&(InitFlag==0)&&(c!=tempc)) {
		  AddressContinueError(lineno);
		  }
  //	Uart_Printf("c=%c  ",c);
  //	Uart_Printf("tempc=%c  ",tempc);
	  tempc=c;
	  save = TRUE;
	  switch (state) {
	  case START:
		if (((c>='A')&&(c<='Z'))||((c>='a')&&(c<='z'))) {
		  state = INADDRESS;
		  } 
		else if ((c>='0')&&(c<='9')) {
		  state = INNUM;
		} 
		else if ((c == '+') || (c == '-')) {
	  state = INSYMBOL;
		}
		else if (( c == ' ') || (c == '\t')) {
	  save = FALSE;
		}
		else if (c == '/') {
			if (CommentFlag==0) {
				save = FALSE;
				//state = INSOLIDUS;
				NotingFlag=1;
		              state = INCOMMENT;
		              currentToken=ENDLINE;
			       EndFlag=1;
				}
			else {
				save=FALSE;
				}
			}
		else {
	  state = DONE;
	  switch (c) {
	  case '$':
		save = FALSE;
		   blankno++;
		   if (blankno==5) {	//五个空行即认为非法结束
			  currentToken = ENDFILE;
			  IlligalEndingError(lineno);
			   ProgramEndFlag=1;
			  }
		   else 
			  {
			  currentToken=ENDLINE;
			  EndFlag=1;
			  }
		break;
	  case '@':
		save = FALSE;
		currentToken = ENDFILE;
		if ((MDI_Flag!=1)||(strlen(lineBuf)!=0)) {
		  IlligalEndingError(lineno);
		  }
		ProgramEndFlag=1;
		break;
	  case '\r':
		save = FALSE;
		currentToken = ENDLINE;
		break;
	  case '#':
		save = FALSE;
		currentToken=VAR;
		break;
	  default:
		currentToken = ERROR;
		break;
	  }
		}
		break;
	  case INADDRESS:
		if ((c>='0')&&(c<='9')) {
	  //backup in the input 
	  ungetNextChar();
	  save = FALSE;
	  state = DONE;
	  currentToken = ADDRESS;
		} 
		else if ( (c == '+') || (c == '-')) {
	  ungetNextChar();
	  save = FALSE;
	  state = DONE;
	  currentToken = ADDRESS;
		}
		else if (c=='#') {
		  ungetNextChar();
		  save=FALSE;
		  state=DONE;
		  currentToken=ADDRESS;
		  }
		else {
	  ungetNextChar();
	  save = FALSE;
	  state = DONE;
	  currentToken = ERROR;
		}
		break;
	/*  case INSOLIDUS:
	  
	if (CommentFlag==0) 
	{
		state = INCOMMENT;
		currentToken=ENDLINE;
		EndFlag=1;
		save = FALSE;
	}
	*/
      break;
	  case INCOMMENT:
		save = FALSE;
		if (c == '\r') {
	  ungetNextChar();
	  state = START;
		}
		break;
	  case INNUM:
		if ((c>='0')&&(c<='9')) {
		  state = INNUM;
		}
		else if (c == '.') {
		  if (ParaBuf[DotProg]==0) {
			  DecimalError(lineno);
			  }
		  state = INPOINT;
		  }
		else {
	  //backup in the input
	  ungetNextChar();
	  save = FALSE;
	  state = DONE;
	  currentToken = NUM;
	  }
		break;
	  case INSYMBOL:
		if ((c>='0')&&(c<='9')) {
	  state=INNUM;
		} 
		else if (c=='#') {
		  save = FALSE;
		  currentToken=VAR;
		  state=DONE;
		  tokenStringIndexFlag=1;
  //		  ungetNextChar();
  //		  save=FALSE;
  //		  currentToken=ADDRESS;
  //		  state=INNUM;
		  }
		else {
	  //backup in the input
	  ungetNextChar();
	  save = FALSE;
	  state = DONE;
	  currentToken = ERROR;
		}
		break;
	  case INPOINT:
		if (!((c>='0')&&(c<='9'))) {
	  //backup in the input
	  ungetNextChar();
	  save = FALSE;
	  state = DONE;
	  currentToken = NUM;
		}
		break;
	  case DONE:
	  default:	 //should never happen
		LCD_Cls();
		//Uart_Printf("scanner Bug: state = %d\n",state);
		state = DONE;
		currentToken = ERROR;
		break;
	  }
  
	  if ((save) && (tokenStringIndex <= MAXTOKENLEN)) {
		tokenString[tokenStringIndex] = c;
		tokenStringIndex++;
	  }
	  if (state == DONE) {
		tokenString[tokenStringIndex] = '\0';
		if (currentToken == ADDRESS) {
	  currentToken = reservedLookup(tokenString);
		}
	  }
	}
  
	if (TraceScan) {
	  LCD_Cls();
	  //Uart_Printf("\t%d:	",lineno);
	  //printToken(currentToken,tokenString);
	}  
	return currentToken;
  } 
  
  
  void BlankMissError(long lineno) {
  //  Uart_Printf("\n>>>");
  //  Uart_Printf(" BlankMissError at line %d\n",lineno);
		 CNC_ERR=140;
		 CNC_IsErr=1;
  }
  
  void AddressContinueError(long lineno) {
  //  Uart_Printf("\n>>>");
  //  Uart_Printf(" AddressContinueError at line %d\n",lineno);
	  CNC_ERR=5;
		 CNC_IsErr=1; 
  
  }
  
  void IlligalEndingError(long lineno) {
  //  Uart_Printf("\n>>>");
  //  Uart_Printf(" Illigal Ending Error at line %d\n",lineno);
	  CNC_ERR=142;
		 CNC_IsErr=1; 
  
  }
  
  void DecimalError(long lineno) {
  //  Uart_Printf("\n>>>");
  //  Uart_Printf(" Illigal Ending Error at line %d\n",lineno);
	  CNC_ERR=7;
		 CNC_IsErr=1; 
  
  }
  
  

⌨️ 快捷键说明

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