Skip to content

syntax.py

from pymwp import Coverage, Variables

Various light syntactic pre-analyses/pre-processors.

  • Coverage checks that input C file follows supported language features.
    • Running pymwp with --strict flag ensures only passing inputs are analyzed.
    • Otherwise, unsupported commands are removed from AST before analysis.
  • FindLoops recursively finds all loop-nodes in an AST.
  • Variables recursively finds variables in an AST.

BaseAnalysis

Bases: NodeHandler

Base implementation for AST analysis.

BIN_OPS class-attribute instance-attribute

BIN_OPS = {PLUS, MINUS, MULT}

Supported binary operators.

U_OPS class-attribute instance-attribute

U_OPS = INC | DEC | {PLUS, MINUS, NEG, SIZEOF}

Supported unary operators.

handler abstractmethod

handler(node: Node, *args, **kwargs) -> None

Handler for AST nodes that meet some abstract criteria.

recurse

recurse(node: Node, *args, **kwargs) -> None

Traverse AST nodes.

Coverage

Coverage(node: Node)

Bases: BaseAnalysis

Simple analysis of AST syntax to determine if AST contains only supported language features.

Attributes:

Name Type Description
node(pr.Node)

AST node.

omit(List[str])

List of unsupported commands.

clear_list(List[Callable])

Functions to clear unsupported syntax.

full property

full: bool

True if entire syntax tree is fully covered by analysis.

ast_mod

ast_mod() -> Coverage

Removes unsupported AST nodes in place.

handler

handler(node: Node, *args, **kwargs)

Make a list of uncovered nodes.

loop_compat staticmethod

loop_compat(node: For) -> Tuple[bool, Optional[str]]

Check if C-language for loop is compatible with an "mwp-loop".

The mwp-loop has form \(\text{loop} \; \texttt{X} \; \{ \texttt{C} \}\). Try to identify if C-language for loop has a similar form, repeat X times command C. The variable X is not allowed to occur in the body C of the loop.

Parameters:

Name Type Description Default
node For

AST node to inspect.

required

Returns:

Type Description
Tuple[bool, Optional[str]]

A tuple where the first item is a compatibility result: True if for loop is mwp-loop compatible, otherwise False. The second is the name of iteration guard variable X, possibly None.

report

report() -> Coverage

Display syntax coverage for inspected AST node.

FindLoops

FindLoops(node: Node)

Bases: BaseAnalysis

Finds all loop nodes in an AST.

Attributes:

Name Type Description
loops List[LoopT]

Loop nodes.

handler

handler(node: LoopT, *args, **kwargs) -> None

Make a (flat) list of the discovered loops.

SyntaxUtils

Various helpful syntax-related utility methods.

array_name staticmethod

array_name(node: ArrayRef) -> str

Find array identifier.

Parameters:

Name Type Description Default
node ArrayRef

Array AST node.

required

Returns:

Type Description
str

Find array name from node.

init_vars staticmethod

init_vars(
    node: Union[Assignment, DeclList, ExprList]
) -> Tuple[List[str], List[str]]

Find and group variables in an init-block.

Looks for declarations/iterators int i=…,… on left and "source" variables …=y on right.

Parameters:

Name Type Description Default
node Union[Assignment, DeclList, ExprList]

Node to inspect.

required

Returns:

Type Description
Tuple[List[str], List[str]]

Discovered variable lists of declarations and sources.

print_mod staticmethod

print_mod(node: Node) -> Node

Prepare AST node for display as string.

For example, for long blocks the body statement is removed. The original node is never modified; if some edit is applied, it is always applied to a copy of the AST node.

Parameters:

Name Type Description Default
node Node

AST node.

required

Returns:

Type Description
Node

AST node conditionally formatted for display.

rm_attr staticmethod

rm_attr(node: Node, attr: str) -> Callable

Construct a callable function to clear an attribute.

Parameters:

Name Type Description Default
node Node

AST node.

required
attr str

Attribute to remove.

required

Returns:

Type Description
Callable

A callable function.

rm_child staticmethod

rm_child(node: Node, attr: str, child: Node) -> Callable

Construct a callable function to remove a child node.

Using a callable allows performing the actual removal later.

Parameters:

Name Type Description Default
node Node

Parent node.

required
attr str

Parent's attribute that contains child, e.g., stmt.

required
child Node

Child node.

required

Returns:

Type Description
Callable

A callable function.

unsupported staticmethod

unsupported(omits: List[str]) -> None

Display unsupported nodes as an ordered list.

Parameters:

Name Type Description Default
omits List[str]

List of unsupported AST nodes.

required

Variables

Variables(*nodes: Node)

Bases: BaseAnalysis

Find variables in an AST.

Attributes:

Name Type Description
vars List[str]

List of variables.

RESERVED class-attribute instance-attribute

RESERVED = ['true', 'false']

List of reserved names that are not variables; see issue #150.

handler

handler(node: Node, *args, **kwargs)

Record the name of a discovered variable.

loop_guard staticmethod

loop_guard(node: For) -> Tuple[List[str], List[str]]

Find variables in a for loop.

Parameters:

Name Type Description Default
node For

A for-loop AST node.

required

Returns:

Type Description
Tuple[List[str], List[str]]

Two lists of variables: (loop_guard, body_variables).