Thread: MPLex problem

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Sweden
    Posts
    263
    Rep Power
    20

    MPLex problem


    Could anyone help me with a MPLex problem.

    I want to find comment blocks like
    Code:
    /*++ BUILD Version: 0091     Increment this if a change has global effects
    Copyright (c) Microsoft Corporation. All rights reserved.
    */
    and #define statements with two or more lines like
    Code:
    #define FILE_GENERIC_READ	(STANDARD_RIGHTS_READ |\
    							FILE_READ_DATA |\
    							FILE_READ_ATTRIBUTES |\
    							FILE_READ_EA |\
    							SYNCHRONIZE)
    in c++ headers.

    The comment blocks is OK but not #define blocks. This is
    so far I have come.
    Code:
    %COMMENT
    %DEFINE
    
    W	[0-9a-zA-Z_]
    S	[ \t\v\n\f\r]
    
    %%
    "/*"				{ BEGIN(COMMENT); return (int)HToken.COMMENTBLOCK; }
    <COMMENT>[^\|*\n]*	{ return (int)HToken.COMMENTBLOCK; }
    <COMMENT>"*/"		{ BEGIN(INITIAL); return (int)HToken.COMMENTBLOCK; }
    
    #define.*"\\"			{ BEGIN(DEFINE); return (int)HToken.DEFINE; }
    <DEFINE>(.*"\\"[^\n])+	{ return (int)HToken.DEFINE; }
    <DEFINE>.*")"			{ BEGIN(INITIAL); return (int)HToken.DEFINE; }
    
    %%
    This is the result of #define block:
    Code:
    #define FILE_GENERIC_READ (STANDARD_RIGHTS_READ |\SYNCHRONIZE)
  2. #2
  3. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi there,

    I'll say at the outset that I am not familiar with MPlex, but since no one replied so far I thought I would try to give you a hand with a fairly standard syntax.

    This expression works using a number of regex flavors: PCRE, Java, Perl, .NET.

    Code:
    (?s)#define[^(]*?\([^)]*?\)
    It matches the define block in your example.

    In other flavors, you could remove the inline flag for "dot-matches new line"---the "(?s)" and turn that on using your local syntax.

    Hoping this works for you. Please let me know.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Sweden
    Posts
    263
    Rep Power
    20
    Thank you for your answer.

    Code:
    #define{space}+{W}+{space}+	{ BEGIN(DEFINE); return (int)NodeToken.DEFINE_BLOCK; }
    <DEFINE>"("{OR}*		{ return (int)NodeToken.DEFINE_BLOCK; }
    <DEFINE>[^\\\r\n]+		{
    				if (yytext.EndsWith("\\"))
    				{
    					return (int)NodeToken.DEFINE_BLOCK;
    				}
    				else
    				{
    					BEGIN(INITIAL); 
    					return (int)NodeToken.DEFINE_BLOCK;
    				}
    				}
    <DEFINE>{OR}*")"$		{ BEGIN(INITIAL); return (int)NodeToken.DEFINE_BLOCK; }
    where {W} is word characters and {OR} also include " " and "|" chars.

    This will match #defines with one or more lines.
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi, you're welcome. Sorry, I don't understand your last post.
    Are you saying that you want to match the pattern in the new box?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Sweden
    Posts
    263
    Rep Power
    20
    My lexer (the new box) will match all #defines for example like these (one or more lines) from a c++ header file.
    Code:
    #define HSHELL_FLASH	(HSHELL_REDRAW|HSHELL_HIGHBIT)
    #define GHND		(GMEM_MOVEABLE | \
    			GMEM_ZEROINIT)
    My parser will then write these lines to a masm include file
    Code:
    HSHELL_FLASH equ (HSHELL_REDRAW or HSHELL_HIGHBIT)
    GHND equ (GMEM_MOVEABLE or GMEM_ZEROINIT)
    The problem is solved.

IMN logo majestic logo threadwatch logo seochat tools logo